private void ReColourImage(JSPImage img, JSPImage.MainColors From, JSPImage.MainColors To) { int numColors = Enum.GetValues( typeof( JSPImage.MainColors ) ).Length; int currentCol = (int) From; int diff = 0; while ( currentCol != (int) To ) { currentCol = ( currentCol + 1 ) % numColors; diff++; } // diff is now how many block-shifts we have to do so that the colors wind up in the right place. byte[] dataWIP = new byte[img.Data.Length]; img.Data.CopyTo( dataWIP, 0 ); for ( int i = 0; i < dataWIP.Length; i++ ) { if ( dataWIP[i] > 32 * (int) From && dataWIP[i] < 32 * (int) From + 31 ) { dataWIP[i] = (byte) ( ( dataWIP[i] + 32 /* Colors per block */ * diff ) % 256 ); } } img.SetData( dataWIP ); }
public static JSPImage FromBitmap( Bitmap image ) { byte[] JSPbytes = new byte[image.Width * image.Height]; int bytesPerPixel = image.PixelFormat == PixelFormat.Format8bppIndexed ? 1 : 3; PixelFormat loadformat; if ( image.PixelFormat == PixelFormat.Format8bppIndexed ) loadformat = PixelFormat.Format8bppIndexed; else loadformat = PixelFormat.Format24bppRgb; BitmapData imgdata = image.LockBits( new Rectangle( 0, 0, image.Width, image.Height ), ImageLockMode.ReadOnly, loadformat ); byte[] imagebytes = new byte[bytesPerPixel * image.Width * image.Height]; int numRows = image.Height; int numBytesPerRow = image.Width * bytesPerPixel; for ( int n = 0; n < image.Height; n++ ) { Marshal.Copy( source: new IntPtr( imgdata.Scan0.ToInt64() + n * imgdata.Stride ), destination: imagebytes, startIndex: n * numBytesPerRow, length: numBytesPerRow ); } if ( image.PixelFormat == PixelFormat.Format8bppIndexed ) { JSPbytes = imagebytes; } else { for ( int i = 0; i < JSPbytes.Length; i++ ) { byte r, g, b; b = imagebytes[3 * i]; g = imagebytes[3 * i + 1]; r = imagebytes[3 * i + 2]; JSPbytes[i] = (byte) CalculateNearestColor( r, g, b ); } } image.UnlockBits( imgdata ); JSPImage output = new JSPImage( (short) image.Width, (short) image.Height ); output.SetData( JSPbytes ); output.SetOffSet( 0, 0 ); return output; }