Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Bitmap.jvm.cs « System.Drawing « System.Drawing « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 90db022df0d0692792abcf88ad54ad0601203a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using System;
using System.IO;
using System.Drawing.Imaging;
using System.Runtime.Serialization;
using Mainsoft.Drawing.Imaging;

using io = java.io;
using imageio = javax.imageio;
using stream = javax.imageio.stream;
using spi = javax.imageio.spi;
using BufferedImage = java.awt.image.BufferedImage;
using JavaImage = java.awt.Image;
using awt = java.awt;
using image = java.awt.image;

namespace System.Drawing 
{
	public sealed class Bitmap : Image {

		# region Static fields

		static readonly image.ColorModel _jpegColorModel = new image.DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);

		#endregion

		#region constructors

		Bitmap (PlainImage orig) {
			base.Initialize( orig, false );
		}

		[MonoTODO]
		private Bitmap (SerializationInfo info, StreamingContext context) {
			throw new NotImplementedException ();
		}

		public Bitmap (int width, int height, Graphics g) 
			:this (width, height, PixelFormat.Format32bppArgb) {
			CurrentImage.HorizontalResolution = g.DpiX;
			CurrentImage.VerticalResolution = g.DpiY;
		}

		public Bitmap (Image original) 
			:this (original, original.Size) {}

		public Bitmap (Image orig, Size newSize)
			:this (orig, newSize.Width, newSize.Height) {}

		public Bitmap (Image orig, int width, int height)
			:base (CreateScaledImage (orig, width, height), ImageFormat.MemoryBmp) {}

		internal Bitmap (java.awt.Image nativeObject, ImageFormat format)
			:base (nativeObject, format) {}

		[MonoTODO]
		private Bitmap (java.awt.Image nativeObject, ImageFormat format, PixelFormat pixFormat)
			:this (nativeObject, format) {
			if (pixFormat != this.PixelFormat)
				throw new NotImplementedException ("Converting PixelFormat is not implemented yet.");
		}

		public Bitmap (int width, int height) 
			:this (width, height, PixelFormat.Format32bppArgb) {}

		public Bitmap (int width, int height, PixelFormat format)
			:base (
			new java.awt.image.BufferedImage (width, height,
			ToBufferedImageFormat (format)),
			ImageFormat.Bmp) {
		}

		public Bitmap (Stream stream)
			:this (stream, false) {}

		public Bitmap (string filename) 
			:this (filename, false) {}

		[MonoTODO]
		public Bitmap (Stream stream, bool useIcm)
			:this (stream, useIcm, null) {}

		[MonoTODO]
		public Bitmap (string filename, bool useIcm)
			:this (filename, useIcm, null) {}

		internal Bitmap (Stream stream, bool useIcm, ImageFormat format) {
			// TBD: useIcm param
			io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
            Initialize (new stream.MemoryCacheImageInputStream (jis), format);
		}

		internal Bitmap (string filename, bool useIcm, ImageFormat format) {
			using(FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
				// TBD: useIcm param
				io.InputStream jis = vmw.common.IOUtils.ToInputStream (stream);
				Initialize (new stream.MemoryCacheImageInputStream (jis), format);
			}
		}

		public Bitmap (Type type, string resource) {
			using (Stream s = type.Assembly.GetManifestResourceStream (resource)) {
				if (s == null)
					throw new ArgumentException("Resource '" + resource + "' could not be found in class '" + type.ToString() + "'");

				io.InputStream jis = vmw.common.IOUtils.ToInputStream (s);
				Initialize (new stream.MemoryCacheImageInputStream (jis), null);
			}
		}
#if INTPTR_SUPPORT
		[MonoTODO]
		public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
		{						
			throw new NotImplementedException();			
		}
#endif
		#endregion

		#region Internal Initialization

		private void Initialize (stream.ImageInputStream input, ImageFormat format) {
			ImageCodec ic = null;

			if (format == null)
				ic = ImageCodec.CreateReader(input);
			else
				ic = ImageCodec.CreateReader(format);

			try {
				ic.NativeStream = input;
				PlainImage pi = ic.ReadPlainImage();
				base.Initialize( pi, false );

				pi = ic.ReadNextPlainImage();
				while ( pi != null) {
					base.Initialize( pi, true );
					pi = ic.ReadNextPlainImage();
				}

				_flags |= (int)(ImageFlags.ImageFlagsReadOnly | ImageFlags.ImageFlagsHasRealPixelSize);
			}
			catch (IOException ex) {
				throw ex;
			}
			catch (Exception) {
				throw new OutOfMemoryException ("Out of memory");
			}
			finally {
				ic.Dispose();
			}
		}

		#endregion

		#region InternalSave
		protected override void InternalSave (stream.ImageOutputStream output, Guid clsid) {

			ImageCodec ic = ImageCodec.CreateWriter( clsid );
			using (ic) {

				PlainImage plainImage = CurrentImage;
				plainImage.NativeImage.flush();

				if ( ImageCodec.ClsidToImageFormat( clsid ).Equals( ImageFormat.Jpeg ) ) {
					image.ColorModel cm = ((image.BufferedImage)CurrentImage.NativeImage).getColorModel();
					if (cm.hasAlpha()) {
						if (cm is image.DirectColorModel) {
							image.Raster raster = ((image.BufferedImage)CurrentImage.NativeImage).getRaster();
							image.DataBuffer db = raster.getDataBuffer();
							image.DirectColorModel dcm = (image.DirectColorModel)cm;
							image.SinglePixelPackedSampleModel jpegSampleModel = new image.SinglePixelPackedSampleModel( 
								db.getDataType(), Width, Height, 
								new int[] {dcm.getRedMask(), dcm.getGreenMask(), dcm.getBlueMask()}	);
		
							image.BufferedImage tb = new image.BufferedImage( 
								_jpegColorModel, 
								image.Raster.createWritableRaster( jpegSampleModel, db, null ),
								false, null );

							plainImage = new PlainImage( tb, plainImage.Thumbnails, ImageFormat.Jpeg, plainImage.HorizontalResolution, plainImage.VerticalResolution, plainImage.Dimension );
							plainImage.NativeMetadata = plainImage.NativeMetadata;
						}
					}
				}

				ic.NativeStream = output;
				ic.WritePlainImage( plainImage );
			}
		}

		#endregion

		#region private statics: ToBufferedImageFormat, CreateScaledImage

		private static int ToBufferedImageFormat (PixelFormat format) {
			switch(format) {
				case PixelFormat.Format16bppGrayScale:
					return BufferedImage.TYPE_USHORT_GRAY;
				case PixelFormat.Format1bppIndexed:
					return BufferedImage.TYPE_BYTE_GRAY;
				case PixelFormat.Format32bppArgb:
					return BufferedImage.TYPE_INT_ARGB;
				case PixelFormat.Format32bppRgb:
					return BufferedImage.TYPE_INT_RGB;
				case PixelFormat.Format32bppPArgb:
					return BufferedImage.TYPE_INT_ARGB_PRE;
				case PixelFormat.Format16bppRgb555:
					return BufferedImage.TYPE_USHORT_555_RGB;
				case PixelFormat.Format16bppRgb565:
					return BufferedImage.TYPE_USHORT_565_RGB;
				case PixelFormat.Indexed:
					return BufferedImage.TYPE_BYTE_INDEXED;
				default:
					return BufferedImage.TYPE_INT_ARGB;
			}			
		}

		private static java.awt.Image CreateScaledImage(Image original, int width, int height) {
			JavaImage oldscaled = original.CurrentImage.NativeImage.getScaledInstance(width, height,
				JavaImage.SCALE_DEFAULT);
			BufferedImage newimage = new BufferedImage(oldscaled.getWidth(null), 
				oldscaled.getHeight(null),
				BufferedImage.TYPE_INT_ARGB);
			java.awt.Graphics2D graphics2d = newimage.createGraphics();
			graphics2d.drawImage(oldscaled, 0, 0, null);
			graphics2d.dispose();
			return newimage;				
		}
		#endregion

		#region Get-SetPixel
		public Color GetPixel (int x, int y) 
		{

			int argb = NativeObject.getRGB(x,y);				
			return Color.FromArgb(argb); 
		}

		public void SetPixel (int x, int y, Color color)
		{				
			int rgb = color.ToArgb();
			NativeObject.setRGB(x,y,rgb);
		}
		#endregion

		#region Clone
		public override object Clone () {
			return new Bitmap ( (PlainImage)CurrentImage.Clone() );
		}

		public Bitmap Clone (Rectangle rect, PixelFormat pixFormat)
		{
			return Clone(new RectangleF( rect.X, rect.Y, rect.Width, rect.Height ), pixFormat);
       	}
		
		public Bitmap Clone (RectangleF rect, PixelFormat pixFormat)
		{
			PlainImage plainImage = CurrentImage.Clone(false);
			BufferedImage clone = new BufferedImage( (int)rect.Width, (int)rect.Height, ToBufferedImageFormat( pixFormat ) );
			awt.Graphics2D g = clone.createGraphics();
			try {
				g.drawImage( NativeObject, -(int)rect.X, -(int)rect.Y, null );
			}
			finally {
				g.dispose();
			}

			plainImage.NativeImage = clone;
			return new Bitmap(plainImage);
		}
		#endregion

		#region LockBits
		[MonoTODO]
		public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format) {
			throw new NotImplementedException();
		}
		#endregion

		#region MakeTransparent
		public void MakeTransparent ()
		{
			Color clr = Color.FromArgb(0,0,0);			
			MakeTransparent (clr);
		}

		public void MakeTransparent (Color transparentColor)
		{
			image.WritableRaster raster = NativeObject.getRaster();
			int numBands  = raster.getNumBands();
			if (numBands != 4)
				return;

			int maxWidth  = raster.getWidth() + raster.getMinX();
			int maxHeight = raster.getHeight() + raster.getMinY();
			int[] srcPix  = new int[numBands];

			for (int y = raster.getMinY(); y < maxHeight; y++) {
				for (int x = raster.getMinX(); x < maxWidth; x++) {
					/*srcPix =*/ raster.getPixel(x, y, srcPix);
					if (srcPix[0] == transparentColor.R &&
						srcPix[1] == transparentColor.G &&
						srcPix[2] == transparentColor.B) {
						srcPix[3] = 0;
						raster.setPixel(x, y, srcPix);
					}
				}
			}
		}
		#endregion

		#region SetResolution
		public void SetResolution (float xDpi, float yDpi)
		{
			CurrentImage.HorizontalResolution = xDpi;
			CurrentImage.VerticalResolution = yDpi;
		}
		#endregion 

		#region UnlockBits
		[MonoTODO]
		public void UnlockBits (BitmapData bitmap_data)
		{
			throw new NotImplementedException();
		}
		#endregion 

		#region NativeObject
		internal new BufferedImage NativeObject {
			get {
				return (BufferedImage)base.NativeObject.CurrentImage.NativeImage;
			}
		}

		protected override java.awt.Image[] CloneNativeObjects(java.awt.Image[] src) {
			if (src == null)
				return null;

			awt.Image[] dst = new awt.Image[src.Length];
			for (int i = 0; i < dst.Length; i++) {
				BufferedImage image = src[i] as BufferedImage;
				if (image == null)
					throw new ArgumentException(String.Format("Unsupported image type '{0}'", src[i].ToString()), "src");

				dst[i] = new BufferedImage(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
			}

			return dst;
		}

		#endregion

		#region InternalPixelFormat
		protected override PixelFormat InternalPixelFormat {
			get {
				int t = NativeObject.getType();
				switch(t) {
					case 11://JavaImage.TYPE_USHORT_GRAY:
						return PixelFormat.Format16bppGrayScale;
					case 10://JavaImage.TYPE_BYTE_GRAY:
						return PixelFormat.Format1bppIndexed;				
					case 1:	//JavaImage.TYPE_INT_RGB
						return PixelFormat.Format32bppRgb;
					case 2: //JavaImage.TYPE_INT_ARGB:			
						return PixelFormat.Format32bppArgb;
					case 3://JavaImage.TYPE_INT_ARGB_PRE:
						return PixelFormat.Format32bppPArgb;
					case 9://JavaImage.TYPE_USHORT_555_RGB:
						return PixelFormat.Format16bppRgb555;
					case 8://JavaImage.TYPE_USHORT_565_RGB:
						return PixelFormat.Format16bppRgb565;
					case 13://JavaImage.TYPE_BYTE_INDEXED:
						return PixelFormat.Indexed;
						//TBD: support this
					case 12://JavaImage.TYPE_BYTE_BINARY:
					case 0://JavaImage.TYPE_CUSTOM:
					case 4://JavaImage.TYPE_INT_BGR:
					case 5://JavaImage.TYPE_3BYTE_BGR:					
					case 6://JavaImage.TYPE_4BYTE_ABGR:
					case 7://JavaImage.TYPE_4BYTE_ABGR_PRE:
					default:
						return PixelFormat.Undefined;
				}			
			}		
		}
		#endregion

#if INTPTR_SUPPORT
		[MonoTODO]
		public static Bitmap FromHicon (IntPtr hicon)
		{	
			throw new NotImplementedException();
		}

		[MonoTODO]
		public static Bitmap FromResource (IntPtr hinstance, string bitmapName)	//TBD: Untested
		{
			throw new NotImplementedException();
		}

		[MonoTODO]
		public IntPtr GetHbitmap ()
		{
			throw new NotImplementedException();
		}

		[MonoTODO]
		public IntPtr GetHbitmap (Color background)
		{
			throw new NotImplementedException();
		}

		[MonoTODO]
		public IntPtr GetHicon ()
		{
			throw new NotImplementedException();
		}
#endif

	}
}