BrawlCrate v0.41
Wii File Editor
Loading...
Searching...
No Matches
Classes | Static Public Member Functions | List of all members
BrawlLib.Internal.Drawing.BitmapExtension Class Reference

Classes

class  NonMonochromeImageException
 

Static Public Member Functions

static Bitmap Resize (this Bitmap i, int width, int height, InterpolationMode mode=InterpolationMode.HighQualityBicubic)
 
static Bitmap Copy (this Bitmap sourceImage)
 
static bool IsIndexed (this Bitmap bmp)
 
static unsafe ColorInformation GetColorInformation (this Bitmap bmp)
 
static void GreyscalePalette (this Bitmap bmp)
 
static Bitmap Quantize (this Bitmap bmp, QuantizationAlgorithm algorithm, int numColors, WiiPixelFormat texFormat, WiiPaletteFormat palFormat, IProgressTracker progress)
 
static unsafe Bitmap IndexColors (this Bitmap src, ColorPalette palette, PixelFormat format)
 
static unsafe void Clamp (this Bitmap bmp, ColorPalette palette)
 
static unsafe void Clamp (this Bitmap bmp, WiiPixelFormat format)
 
static void CopyTo (this Bitmap bmp, Bitmap dest)
 
static unsafe Bitmap Clone (this Bitmap src, int width, int height, int skip)
 
static unsafe Bitmap GenerateMip (this Bitmap bmp, int level)
 
static unsafe Bitmap InvertColors (this Bitmap bmp)
 
static unsafe Bitmap InvertAlpha (this Bitmap bmp)
 
static unsafe Bitmap SwapAlphaAndRGB (this Bitmap bmp)
 
static unsafe bool GuessIfAlphaInverted (this Bitmap bmp)
 Guess if the alpha channel of an image might be inverted. (This method should prevent false positives as much as possible.) More...
 
static void SaveTGA (this Bitmap bmp, string path)
 
static void SaveTGA (this Bitmap bmp, FileStream stream)
 

Member Function Documentation

◆ Clamp() [1/2]

static unsafe void BrawlLib.Internal.Drawing.BitmapExtension.Clamp ( this Bitmap  bmp,
ColorPalette  palette 
)
inlinestatic
228 {
229 int w = bmp.Width, h = bmp.Height, e = palette.Entries.Length;
230 BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
231 PixelFormat.Format32bppArgb);
232
233 for (ARGBPixel* ptr = (ARGBPixel*) data.Scan0, ceil = ptr + w * h; ptr < ceil;)
234 {
235 *ptr = (ARGBPixel) palette.Entries[palette.FindMatch(*ptr++)];
236
237 //ARGBPixel p = *ptr;
238 //int bestDist = Int32.MaxValue, bestIndex = 0;
239 //for(int i = 0 ; i < e ; i++)
240 //{
241 // int dist = p.DistanceTo(palette.Entries[i]);
242 // if(dist < bestDist)
243 // {
244 // bestDist = dist;
245 // bestIndex = i;
246 // if (dist == 0) break;
247 // }
248 //}
249 //*ptr++ = (ARGBPixel)palette.Entries[bestIndex];
250 }
251
252 bmp.UnlockBits(data);
253 }
Definition: PixelTypes.cs:16

◆ Clamp() [2/2]

static unsafe void BrawlLib.Internal.Drawing.BitmapExtension.Clamp ( this Bitmap  bmp,
WiiPixelFormat  format 
)
inlinestatic
258 {
259 int w = bmp.Width, h = bmp.Height;
260 BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
261 PixelFormat.Format32bppArgb);
262
263 PixelClamper clmp;
264 switch (format)
265 {
266 case WiiPixelFormat.I4:
267 {
268 clmp = p => { *p = ((I4Pixel) (*p))[0]; };
269 break;
270 }
271
272 case WiiPixelFormat.I8:
273 {
274 clmp = p => { *p = (ARGBPixel) (I8Pixel) (*p); };
275 break;
276 }
277
278 case WiiPixelFormat.IA4:
279 {
280 clmp = p => { *p = (IA4Pixel) (*p); };
281 break;
282 }
283
284 case WiiPixelFormat.IA8:
285 {
286 clmp = p => { *p = (IA8Pixel) (*p); };
287 break;
288 }
289
290 case WiiPixelFormat.RGB565:
291 {
292 clmp = p => { *p = (ARGBPixel) (wRGB565Pixel) (*p); };
293 break;
294 }
295
296 case WiiPixelFormat.RGB5A3:
297 {
298 clmp = p => { *p = (ARGBPixel) (wRGB5A3Pixel) (*p); };
299 break;
300 }
301
302 case WiiPixelFormat.RGBA8:
303 default:
304 {
305 clmp = p => { };
306 break;
307 }
308 }
309
310 for (ARGBPixel* ptr = (ARGBPixel*) data.Scan0, ceil = ptr + w * h; ptr < ceil;)
311 {
312 clmp(ptr++);
313 }
314
315 bmp.UnlockBits(data);
316 }
WiiPixelFormat
Definition: Enum.cs:4
Definition: PixelTypes.cs:100
Definition: PixelTypes.cs:231

◆ Clone()

static unsafe Bitmap BrawlLib.Internal.Drawing.BitmapExtension.Clone ( this Bitmap  src,
int  width,
int  height,
int  skip 
)
inlinestatic
330 {
331 int sw = src.Width, sh = src.Height;
332 PixelFormat format = src.PixelFormat;
333 Bitmap dst = new Bitmap(width, height, format);
334
335 BitmapData srcData = src.LockBits(new Rectangle(0, 0, sw, sh), ImageLockMode.ReadOnly, format);
336 BitmapData dstData = dst.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, format);
337
338 switch (format)
339 {
340 case PixelFormat.Format4bppIndexed:
341 {
342 for (int sy = skip / 2, dy = 0; sy < sh && dy < height; dy++, sy += skip)
343 {
344 byte* sPtr = (byte*) srcData.Scan0 + sy * srcData.Stride;
345 byte* dPtr = (byte*) dstData.Scan0 + dy * dstData.Stride;
346 for (int sx = skip / 2, dx = 0; sx < sw && dx < width; dx++, sx += skip)
347 {
348 byte value = sx % 2 == 0 ? (byte) (sPtr[sx >> 1] >> 4) : (byte) (sPtr[sx >> 1] & 0x0F);
349 dPtr[dx >> 1] = dx % 2 == 0
350 ? (byte) ((value << 4) | (dPtr[dx >> 1] & 0x0F))
351 : (byte) ((dPtr[dx >> 1] & 0xF0) | value);
352 }
353 }
354
355 break;
356 }
357 }
358
359 dst.UnlockBits(dstData);
360 src.UnlockBits(srcData);
361
362 return dst;
363 }

◆ Copy()

static Bitmap BrawlLib.Internal.Drawing.BitmapExtension.Copy ( this Bitmap  sourceImage)
inlinestatic
41 {
42 if (sourceImage.Palette.Entries.Length > 0)
43 {
44 //Indexed
45 Bitmap targetImage = new Bitmap(sourceImage.Width, sourceImage.Height,
46 sourceImage.PixelFormat);
47 BitmapData sourceData = sourceImage.LockBits(
48 new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
49 ImageLockMode.ReadOnly, sourceImage.PixelFormat);
50 BitmapData targetData = targetImage.LockBits(
51 new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
52 ImageLockMode.WriteOnly, targetImage.PixelFormat);
53 Memory.Move(targetData.Scan0, sourceData.Scan0, (uint) sourceData.Stride * (uint) sourceData.Height);
54 sourceImage.UnlockBits(sourceData);
55 targetImage.UnlockBits(targetData);
56 targetImage.Palette = sourceImage.Palette;
57 return targetImage;
58 }
59 else
60 {
61 //Non-indexed
62 Bitmap targetImage = new Bitmap(sourceImage.Width, sourceImage.Height, sourceImage.PixelFormat);
63 targetImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
64 using (Graphics g = Graphics.FromImage(targetImage))
65 {
66 g.DrawImageUnscaled(sourceImage, 0, 0);
67 }
68
69 return targetImage;
70 }
71 }

◆ CopyTo()

static void BrawlLib.Internal.Drawing.BitmapExtension.CopyTo ( this Bitmap  bmp,
Bitmap  dest 
)
inlinestatic
319 {
320 int w = Math.Min(bmp.Width, dest.Width);
321 int h = Math.Min(bmp.Height, dest.Height);
322 using (DIB dib = new DIB(w, h, dest.PixelFormat))
323 {
324 dib.ReadBitmap(bmp, w, h);
325 dib.WriteBitmap(dest, w, h);
326 }
327 }

◆ GenerateMip()

static unsafe Bitmap BrawlLib.Internal.Drawing.BitmapExtension.GenerateMip ( this Bitmap  bmp,
int  level 
)
inlinestatic
366 {
367 if (level <= 1)
368 {
369 return (Bitmap) bmp.Clone();
370 }
371
372 int scale = 1 << (level - 1);
373 int w = bmp.Width / scale, h = bmp.Height / scale;
374
375 Bitmap dst = new Bitmap(w, h, bmp.PixelFormat);
376
377 //Step-scale indexed elements
378 if (bmp.IsIndexed())
379 {
380 BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
381 bmp.PixelFormat);
382 BitmapData dstData = dst.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, bmp.PixelFormat);
383
384 float xStep = (float) bmp.Width / w;
385 float yStep = (float) bmp.Height / h;
386 int x, y;
387 float fx, fy;
388
389 byte* sPtr, dPtr = (byte*) dstData.Scan0;
390 if (bmp.PixelFormat == PixelFormat.Format8bppIndexed)
391 {
392 for (y = 0, fy = 0.5f; y < h; y++, fy += yStep, dPtr += dstData.Stride)
393 {
394 sPtr = (byte*) srcData.Scan0 + (int) fy * srcData.Stride;
395 for (x = 0, fx = 0.5f; x < w; x++, fx += xStep)
396 {
397 dPtr[x] = sPtr[(int) fx];
398 }
399 }
400 }
401 else
402 {
403 for (y = 0, fy = 0.5f; y < h; y++, fy += yStep, dPtr += dstData.Stride)
404 {
405 sPtr = (byte*) srcData.Scan0 + (int) fy * srcData.Stride;
406 int b = 0, ind;
407 for (x = 0, fx = 0.5f; x < w; x++, fx += xStep)
408 {
409 ind = (int) fx;
410 if ((x & 1) == 0)
411 {
412 if ((ind & 1) == 0)
413 {
414 b = sPtr[ind >> 1] & 0xF0;
415 }
416 else
417 {
418 b = sPtr[ind >> 1] << 4;
419 }
420 }
421 else
422 {
423 if ((ind & 1) == 0)
424 {
425 b |= sPtr[ind >> 1] >> 4;
426 }
427 else
428 {
429 b |= sPtr[ind >> 1] & 0xF;
430 }
431
432 dPtr[x >> 1] = (byte) b;
433 }
434 }
435
436 if ((x & 1) != 0)
437 {
438 dPtr[x >> 1] = (byte) b;
439 }
440 }
441 }
442
443 bmp.UnlockBits(srcData);
444 dst.UnlockBits(dstData);
445 }
446 else
447 {
448 using (Graphics g = Graphics.FromImage(dst))
449 {
450 g.CompositingMode = CompositingMode.SourceCopy;
451 g.CompositingQuality = CompositingQuality.HighQuality;
452 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
453 g.PixelOffsetMode = PixelOffsetMode.HighQuality;
454 g.SmoothingMode = SmoothingMode.AntiAlias;
455
456 g.DrawImage(bmp, new Rectangle(0, 0, w, h));
457 }
458 }
459
460 return dst;
461 }

◆ GetColorInformation()

static unsafe ColorInformation BrawlLib.Internal.Drawing.BitmapExtension.GetColorInformation ( this Bitmap  bmp)
inlinestatic
85 {
86 HashSet<ARGBPixel> colors = new HashSet<ARGBPixel>();
87
88 BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
89 PixelFormat.Format32bppArgb);
90
91 for (ARGBPixel* ptr = (ARGBPixel*) data.Scan0, c = ptr + bmp.Width * bmp.Height; ptr < c;)
92 {
93 colors.Add(*ptr++);
94 }
95
96 bmp.UnlockBits(data);
97
98 return new ColorInformation(colors.ToArray());
99 }

◆ GreyscalePalette()

static void BrawlLib.Internal.Drawing.BitmapExtension.GreyscalePalette ( this Bitmap  bmp)
inlinestatic
102 {
103 ColorPalette pal = bmp.Palette;
104
105 float inc = 255.0f / pal.Entries.Length, c = 0;
106 for (int i = 0, z = 0; i < pal.Entries.Length; i++, c += inc, z = (int) c)
107 {
108 pal.Entries[i] = Color.FromArgb(z, z, z);
109 }
110
111 bmp.Palette = pal;
112 }

◆ GuessIfAlphaInverted()

static unsafe bool BrawlLib.Internal.Drawing.BitmapExtension.GuessIfAlphaInverted ( this Bitmap  bmp)
inlinestatic

Guess if the alpha channel of an image might be inverted. (This method should prevent false positives as much as possible.)

Returns
true if the number of distinct pixel values in the fully transparent section of the image is more than the number elsewhere (including alpha values); false otherwise.
628 {
629 int[] pixels;
630
631 if (bmp.PixelFormat == PixelFormat.Format32bppArgb)
632 {
633 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
634 BitmapData inData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
635
636 uint* inPtr = (uint*) inData.Scan0;
637
638 int length = Math.Abs(inData.Stride) * bmp.Height / sizeof(uint);
639
640 pixels = new int[length];
641 System.Runtime.InteropServices.Marshal.Copy((IntPtr) inPtr, pixels, 0, length);
642
643 bmp.UnlockBits(inData);
644 }
645 else
646 {
647 // Slower, but works for any input pixel format
648 pixels = new int[bmp.Width * bmp.Height];
649 int i = 0;
650 for (int x = 0; x < bmp.Width; x++)
651 {
652 for (int y = 0; y < bmp.Height; y++)
653 {
654 Color c = bmp.GetPixel(x, y);
655 pixels[i++] = c.ToArgb();
656 }
657 }
658 }
659
660 HashSet<int> colorsInTransparentSection = new HashSet<int>();
661 HashSet<int> colorsInNonTransparenSection = new HashSet<int>();
662
663 foreach (int pixel in pixels)
664 {
665 byte alpha = (byte) ((pixel & 0xFF000000) >> 24);
666 if (alpha == 0)
667 {
668 colorsInTransparentSection.Add(pixel);
669 }
670 else
671 {
672 colorsInNonTransparenSection.Add(pixel);
673 }
674 }
675
676 return colorsInTransparentSection.Count > colorsInNonTransparenSection.Count
677 && colorsInTransparentSection.Count > 1;
678 }

◆ IndexColors()

static unsafe Bitmap BrawlLib.Internal.Drawing.BitmapExtension.IndexColors ( this Bitmap  src,
ColorPalette  palette,
PixelFormat  format 
)
inlinestatic
150 {
151 int w = src.Width, h = src.Height;
152
153 int entries = palette.Entries.Length;
154 switch (format)
155 {
156 case PixelFormat.Format4bppIndexed:
157 {
158 entries = Math.Min(entries, 16);
159 break;
160 }
161
162 case PixelFormat.Format8bppIndexed:
163 {
164 entries = Math.Min(entries, 256);
165 break;
166 }
167
168 default:
169 {
170 throw new ArgumentException("Pixel format is not an indexed format.");
171 }
172 }
173
174 Bitmap dst = new Bitmap(w, h, format)
175 {
176 Palette = palette
177 };
178
179 BitmapData sData = src.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly,
180 PixelFormat.Format32bppArgb);
181 BitmapData dData = dst.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, format);
182
183 ARGBPixel* sPtr = (ARGBPixel*) sData.Scan0;
184 for (int y = 0; y < h; y++)
185 {
186 byte* dPtr = (byte*) dData.Scan0 + y * dData.Stride;
187 for (int x = 0; x < w; x++)
188 {
189 ARGBPixel p = *sPtr++;
190
191 int bestDist = int.MaxValue, bestIndex = 0;
192 for (int z = 0; z < entries; z++)
193 {
194 int dist = p.DistanceTo(palette.Entries[z]);
195 if (dist < bestDist)
196 {
197 bestDist = dist;
198 bestIndex = z;
199 }
200 }
201
202 if (format == PixelFormat.Format4bppIndexed)
203 {
204 byte val = *dPtr;
205 if (x % 2 == 0)
206 {
207 *dPtr = (byte) ((bestIndex << 4) | (val & 0x0F));
208 }
209 else
210 {
211 *dPtr++ = (byte) ((val & 0xF0) | (bestIndex & 0x0F));
212 }
213 }
214 else
215 {
216 *dPtr++ = (byte) bestIndex;
217 }
218 }
219 }
220
221 dst.UnlockBits(dData);
222 src.UnlockBits(sData);
223
224 return dst;
225 }
int DistanceTo(Color c)
Definition: PixelTypes.cs:46

◆ InvertAlpha()

static unsafe Bitmap BrawlLib.Internal.Drawing.BitmapExtension.InvertAlpha ( this Bitmap  bmp)
inlinestatic
507 {
508 if (bmp.PixelFormat == PixelFormat.Format32bppArgb)
509 {
510 Bitmap output = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format32bppArgb);
511
512 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
513 BitmapData inData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
514 BitmapData outData = output.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
515
516 uint* inPtr = (uint*) inData.Scan0;
517 uint* outPtr = (uint*) outData.Scan0;
518
519 int length = Math.Abs(inData.Stride) * bmp.Height / sizeof(uint);
520
521 for (int i = 0; i < length; i++)
522 {
523 outPtr[i] = inPtr[i] ^ 0xFF000000;
524 }
525
526 bmp.UnlockBits(inData);
527 output.UnlockBits(outData);
528
529 return output;
530 }
531 else
532 {
533 // Slower, but works for any input pixel format
534 Bitmap output = new Bitmap(bmp.Width, bmp.Height);
535 for (int x = 0; x < bmp.Width; x++)
536 {
537 for (int y = 0; y < bmp.Height; y++)
538 {
539 Color c = bmp.GetPixel(x, y);
540 c = Color.FromArgb((byte) ~c.A, c);
541 output.SetPixel(x, y, c);
542 }
543 }
544
545 return output;
546 }
547 }

◆ InvertColors()

static unsafe Bitmap BrawlLib.Internal.Drawing.BitmapExtension.InvertColors ( this Bitmap  bmp)
inlinestatic
464 {
465 if (bmp.PixelFormat == PixelFormat.Format32bppArgb)
466 {
467 Bitmap output = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format32bppArgb);
468
469 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
470 BitmapData inData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
471 BitmapData outData = output.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
472
473 uint* inPtr = (uint*) inData.Scan0;
474 uint* outPtr = (uint*) outData.Scan0;
475
476 int length = Math.Abs(inData.Stride) * bmp.Height / sizeof(uint);
477
478 for (int i = 0; i < length; i++)
479 {
480 outPtr[i] = inPtr[i] ^ 0xFFFFFF;
481 }
482
483 bmp.UnlockBits(inData);
484 output.UnlockBits(outData);
485
486 return output;
487 }
488 else
489 {
490 // Slower, but works for any input pixel format
491 Bitmap output = new Bitmap(bmp.Width, bmp.Height);
492 for (int x = 0; x < bmp.Width; x++)
493 {
494 for (int y = 0; y < bmp.Height; y++)
495 {
496 Color c = bmp.GetPixel(x, y);
497 c = Color.FromArgb(c.A, (byte) ~c.R, (byte) ~c.G, (byte) ~c.B);
498 output.SetPixel(x, y, c);
499 }
500 }
501
502 return output;
503 }
504 }

◆ IsIndexed()

static bool BrawlLib.Internal.Drawing.BitmapExtension.IsIndexed ( this Bitmap  bmp)
inlinestatic
74 {
75 return (bmp.PixelFormat & PixelFormat.Indexed) != 0;
76 }

◆ Quantize()

static Bitmap BrawlLib.Internal.Drawing.BitmapExtension.Quantize ( this Bitmap  bmp,
QuantizationAlgorithm  algorithm,
int  numColors,
WiiPixelFormat  texFormat,
WiiPaletteFormat  palFormat,
IProgressTracker  progress 
)
inlinestatic
145 {
146 return MedianCut.Quantize(bmp, numColors, texFormat, palFormat, progress);
147 }

◆ Resize()

static Bitmap BrawlLib.Internal.Drawing.BitmapExtension.Resize ( this Bitmap  i,
int  width,
int  height,
InterpolationMode  mode = InterpolationMode.HighQualityBicubic 
)
inlinestatic
19 {
20 Bitmap r = new Bitmap(width, height);
21 r.SetResolution(i.HorizontalResolution, i.VerticalResolution);
22 using (Graphics graphics = Graphics.FromImage(r))
23 {
24 graphics.InterpolationMode = mode;
25 graphics.CompositingQuality = CompositingQuality.HighQuality;
26 graphics.CompositingMode = CompositingMode.SourceCopy;
27 graphics.SmoothingMode = SmoothingMode.HighQuality;
28 using (ImageAttributes wrapMode = new ImageAttributes())
29 {
30 //This fixes the antialiasing on the edges of the image
31 wrapMode.SetWrapMode(WrapMode.TileFlipXY);
32 graphics.DrawImage(i, new Rectangle(0, 0, r.Width, r.Height), 0, 0, i.Width, i.Height,
33 GraphicsUnit.Pixel, wrapMode);
34 }
35 }
36
37 return r;
38 }

◆ SaveTGA() [1/2]

static void BrawlLib.Internal.Drawing.BitmapExtension.SaveTGA ( this Bitmap  bmp,
FileStream  stream 
)
inlinestatic
686 {
687 TGA.ToStream(bmp, stream);
688 }
Definition: TGAImage.cs:120
static unsafe void ToStream(Bitmap bmp, FileStream stream)
Definition: TGAImage.cs:358

◆ SaveTGA() [2/2]

static void BrawlLib.Internal.Drawing.BitmapExtension.SaveTGA ( this Bitmap  bmp,
string  path 
)
inlinestatic
681 {
682 TGA.ToFile(bmp, path);
683 }
static void ToFile(Bitmap bmp, string path)
Definition: TGAImage.cs:516

◆ SwapAlphaAndRGB()

static unsafe Bitmap BrawlLib.Internal.Drawing.BitmapExtension.SwapAlphaAndRGB ( this Bitmap  bmp)
inlinestatic
557 {
558 if (bmp.PixelFormat == PixelFormat.Format32bppArgb)
559 {
560 Bitmap output = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format32bppArgb);
561
562 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
563 BitmapData inData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
564 BitmapData outData = output.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
565
566 uint* inPtr = (uint*) inData.Scan0;
567 uint* outPtr = (uint*) outData.Scan0;
568
569 int length = Math.Abs(inData.Stride) * bmp.Height / sizeof(uint);
570
571 try
572 {
573 for (int i = 0; i < length; i++)
574 {
575 byte A = (byte) (inPtr[i] >> 24);
576 byte R = (byte) (inPtr[i] >> 16);
577 byte G = (byte) (inPtr[i] >> 8);
578 byte B = (byte) inPtr[i];
579 if (R != G || G != B)
580 {
581 throw new NonMonochromeImageException(
582 "Cannot swap alpha and value channels on a non-monochrome image.");
583 }
584
585 outPtr[i] = (uint) ((R << 24) | (A << 16) | (A << 8) | A);
586 }
587 }
588 finally
589 {
590 bmp.UnlockBits(inData);
591 output.UnlockBits(outData);
592 }
593
594 return output;
595 }
596 else
597 {
598 // Slower, but works for any input pixel format
599 Bitmap output = new Bitmap(bmp.Width, bmp.Height);
600 for (int x = 0; x < bmp.Width; x++)
601 {
602 for (int y = 0; y < bmp.Height; y++)
603 {
604 Color c = bmp.GetPixel(x, y);
605 if (c.R != c.G || c.G != c.B)
606 {
607 throw new NonMonochromeImageException(
608 "Cannot swap alpha and value channels on a non-monochrome image.");
609 }
610
611 c = Color.FromArgb(c.R, c.A, c.A, c.A);
612 output.SetPixel(x, y, c);
613 }
614 }
615
616 return output;
617 }
618 }

The documentation for this class was generated from the following file: