|
| 1 | +// Copyright (c) 2007-2016 CSJ2K contributors. |
| 2 | +// Licensed under the BSD 3-Clause License. |
| 3 | + |
| 4 | +namespace CSJ2K.Util |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Drawing; |
| 8 | + using System.Drawing.Imaging; |
| 9 | + using System.Linq; |
| 10 | + |
| 11 | + using CSJ2K.j2k.image; |
| 12 | + |
| 13 | + internal class BitmapImageSource : PortableImageSource |
| 14 | + { |
| 15 | + #region CONSTRUCTORS |
| 16 | + |
| 17 | + private BitmapImageSource(Bitmap bitmap) |
| 18 | + : base( |
| 19 | + bitmap.Width, |
| 20 | + bitmap.Height, |
| 21 | + GetNumberOfComponents(bitmap.PixelFormat), |
| 22 | + GetRangeBits(bitmap.PixelFormat), |
| 23 | + GetSignedArray(bitmap.PixelFormat), |
| 24 | + GetComponents(bitmap)) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + #endregion |
| 29 | + |
| 30 | + #region METHODS |
| 31 | + |
| 32 | + |
| 33 | + internal static BlkImgDataSrc Create(object imageObject) |
| 34 | + { |
| 35 | + var bitmap = imageObject as Bitmap; |
| 36 | + return bitmap == null ? null : new BitmapImageSource(bitmap); |
| 37 | + } |
| 38 | + |
| 39 | + private static int GetNumberOfComponents(PixelFormat pixelFormat) |
| 40 | + { |
| 41 | + switch (pixelFormat) |
| 42 | + { |
| 43 | + case PixelFormat.Format16bppGrayScale: |
| 44 | + case PixelFormat.Format1bppIndexed: |
| 45 | + case PixelFormat.Format4bppIndexed: |
| 46 | + case PixelFormat.Format8bppIndexed: |
| 47 | + return 1; |
| 48 | + case PixelFormat.Format24bppRgb: |
| 49 | + case PixelFormat.Format32bppArgb: |
| 50 | + case PixelFormat.Format32bppPArgb: |
| 51 | + case PixelFormat.Format32bppRgb: |
| 52 | + return 3; |
| 53 | + default: |
| 54 | + throw new ArgumentOutOfRangeException("pixelFormat"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static int GetRangeBits(PixelFormat pixelFormat) |
| 59 | + { |
| 60 | + switch (pixelFormat) |
| 61 | + { |
| 62 | + case PixelFormat.Format16bppGrayScale: |
| 63 | + return 16; |
| 64 | + case PixelFormat.Format1bppIndexed: |
| 65 | + return 1; |
| 66 | + case PixelFormat.Format4bppIndexed: |
| 67 | + return 4; |
| 68 | + case PixelFormat.Format8bppIndexed: |
| 69 | + case PixelFormat.Format24bppRgb: |
| 70 | + case PixelFormat.Format32bppArgb: |
| 71 | + case PixelFormat.Format32bppPArgb: |
| 72 | + case PixelFormat.Format32bppRgb: |
| 73 | + return 8; |
| 74 | + default: |
| 75 | + throw new ArgumentOutOfRangeException("pixelFormat"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static bool[] GetSignedArray(PixelFormat pixelFormat) |
| 80 | + { |
| 81 | + return Enumerable.Repeat(false, GetNumberOfComponents(pixelFormat)).ToArray(); |
| 82 | + } |
| 83 | + |
| 84 | + private static int[][] GetComponents(Bitmap bitmap) |
| 85 | + { |
| 86 | + var w = bitmap.Width; |
| 87 | + var h = bitmap.Height; |
| 88 | + var nc = GetNumberOfComponents(bitmap.PixelFormat); |
| 89 | + |
| 90 | + var comps = new int[nc][]; |
| 91 | + for (var c = 0; c < nc; ++c) comps[c] = new int[w * h]; |
| 92 | + |
| 93 | + for (int y = 0, xy = 0; y < h; ++y) |
| 94 | + { |
| 95 | + for (var x = 0; x < w; ++x, ++xy) |
| 96 | + { |
| 97 | + var color = bitmap.GetPixel(x, y); |
| 98 | + for (var c = 0; c < nc; ++c) |
| 99 | + { |
| 100 | + comps[c][xy] = c == 0 ? color.R : c == 1 ? color.G : color.B; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return comps; |
| 106 | + } |
| 107 | + |
| 108 | + #endregion |
| 109 | + } |
| 110 | +} |
0 commit comments