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
|
/// Copyright (c) 2008 Jeffrey Powers for Fluxcapacity Open Source.
/// Under the MIT License, details: License.txt.
using System;
#if SILVERLIGHT
#else
using System.Drawing;
using System.Drawing.Imaging;
#endif
namespace FluxJpeg.Core {
public struct ColorModel {
public ColorSpace colorspace;
public bool Opaque;
}
public enum ColorSpace { Gray, YCbCr, RGB }
public class Image {
private ColorModel _cm;
private byte[][,] _raster;
public byte[][,] Raster { get { return _raster; } }
public ColorModel ColorModel { get { return _cm; } }
/// <summary> X density (dots per inch).</summary>
public double DensityX { get; set; }
/// <summary> Y density (dots per inch).</summary>
public double DensityY { get; set; }
public int ComponentCount { get { return _raster.Length; } }
/// <summary>
/// Converts the colorspace of an image (in-place)
/// </summary>
/// <param name="cs">Colorspace to convert into</param>
/// <returns>Self</returns>
public Image ChangeColorSpace(ColorSpace cs) {
// Colorspace is already correct
if (_cm.colorspace == cs) return this;
byte[] ycbcr = new byte[3];
byte[] rgb = new byte[3];
if (_cm.colorspace == ColorSpace.RGB && cs == ColorSpace.YCbCr) {
/*
* Y' = + 0.299 * R'd + 0.587 * G'd + 0.114 * B'd
Cb = 128 - 0.168736 * R'd - 0.331264 * G'd + 0.5 * B'd
Cr = 128 + 0.5 * R'd - 0.418688 * G'd - 0.081312 * B'd
*
*/
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
YCbCr.fromRGB(ref _raster[0][x, y], ref _raster[1][x, y], ref _raster[2][x, y]);
}
_cm.colorspace = ColorSpace.YCbCr;
} else if (_cm.colorspace == ColorSpace.YCbCr && cs == ColorSpace.RGB) {
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
// 0 is LUMA
// 1 is BLUE
// 2 is RED
YCbCr.toRGB(ref _raster[0][x, y], ref _raster[1][x, y], ref _raster[2][x, y]);
}
_cm.colorspace = ColorSpace.RGB;
} else if (_cm.colorspace == ColorSpace.Gray && cs == ColorSpace.YCbCr) {
// To convert to YCbCr, we just add two 128-filled chroma channels
byte[,] Cb = new byte[width, height];
byte[,] Cr = new byte[width, height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
Cb[x, y] = 128; Cr[x, y] = 128;
}
_raster = new byte[][,] { _raster[0], Cb, Cr };
_cm.colorspace = ColorSpace.YCbCr;
} else if (_cm.colorspace == ColorSpace.Gray && cs == ColorSpace.RGB) {
ChangeColorSpace(ColorSpace.YCbCr);
ChangeColorSpace(ColorSpace.RGB);
} else {
throw new Exception("Colorspace conversion not supported.");
}
return this;
}
private int width; private int height;
public int Width { get { return width; } }
public int Height { get { return height; } }
public Image(ColorModel cm, byte[][,] raster) {
width = raster[0].GetLength(0);
height = raster[0].GetLength(1);
_cm = cm;
_raster = raster;
}
public static byte[][,] CreateRaster(int width, int height, int bands) {
// Create the raster
byte[][,] raster = new byte[bands][,];
for (int b = 0; b < bands; b++)
raster[b] = new byte[width, height];
return raster;
}
delegate void ConvertColor(ref byte c1, ref byte c2, ref byte c3);
#if SILVERLIGHT
#else
public Bitmap ToBitmap()
{
ConvertColor ColorConverter;
switch(_cm.colorspace)
{
case ColorSpace.YCbCr:
ColorConverter = YCbCr.toRGB;
break;
default:
throw new Exception("Colorspace not supported yet.");
}
int _width = width;
int _height = height;
Bitmap bitmap = new Bitmap(_width, _height, PixelFormat.Format32bppArgb);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
byte[] outColor = new byte[3];
byte[] inColor = new byte[3];
unsafe
{
int i = 0;
byte* ptrBitmap = (byte*)bmData.Scan0;
for (int y = 0; y < _height; y++)
{
for (int x = 0; x < _width; x++)
{
ptrBitmap[0] = (byte)_raster[0][x, y];
ptrBitmap[1] = (byte)_raster[1][x, y];
ptrBitmap[2] = (byte)_raster[2][x, y];
ColorConverter(ref ptrBitmap[0], ref ptrBitmap[1], ref ptrBitmap[2]);
// Swap RGB --> BGR
byte R = ptrBitmap[0];
ptrBitmap[0] = ptrBitmap[2];
ptrBitmap[2] = R;
ptrBitmap[3] = 255; /* 100% opacity */
ptrBitmap += 4; // advance to the next pixel
i++; // "
}
}
}
bitmap.UnlockBits(bmData);
return bitmap;
}
#endif
}
}
|