Silverlight 3 Writeable Bitmap API - Plasma

plasma1

Updated on 15/07/2009 to Silverlight 3 RTW

Inspired by old school MS DOS demos and this Flash Alchemy experiment.
Found out that someone already ported some code from the Alchemy experiment to C#.
So after some tweaking and some help of my colleague Frederik it’s up and running in Silverlight 3 beta.

I’m using two threads to speed up the plasma calculation when moving the dots, since everybody has a duo,triple,quad,octa … core CPU these days,
One thread does even lines and the other odd lines (interlacing).

 public static void GeneratePlasmaEvenLines()
     {
       int index = 0;

       for (var x = 0; x < width; x +=2)
       {
         for (var y = 0; y < height ; y+=1)
         {
           uint color = CalculatePixel(x, y);
           plasma[index++] = color % 255;
         }
         index += width;
       }
     }

     public static void GeneratePlasmaOddLines()
     {
         int index = width;
         for (var x = 1; x < width; x+=2)
         {
             for (var y = 0; y < height; y+=1)
             {
                 uint color = CalculatePixel(x, y);
                 plasma[index++] = color % 255;
             }
             index += height;
         }
     }

  private static uint CalculatePixel(int x, int y)
     {
         int w = (int) param6;
         int h = (int)param5;
         uint color = (uint)
        (
              128.0 + (param3 * Math.Sin(x / param1))
            + 128.0 + (param4 * Math.Sin(y / param2))
            + 128.0 + ((param5/4) * Math.Sin(Math.Sqrt((double)((x - w)* (x - w) + (y - h ) * (y - h))) / 9.0))
            + 128.0 + ((param6/4)* Math.Sin(Math.Sqrt((double)(x * x + y * y)) / 9.0))
        ) / 4;

         // Add shift position to sync the color
         color +=  LastShift;
         return color;
     }

Placing the pixels on the canvas with the our brand new bitmap api

private void BuildBitmap(uint[] data)
        {
            uint colorValue=0;
                for (int x = 0; x < imageWidth; x++)
                {
                    for (int y = 0; y < imageHeight; y++)
                    {
                        // set the pixel value
                        int palletIndex = (int)data[y * imageWidth + x];
                        colorValue = GeneratePlasma.palette[palletIndex];
                        writeableBitmap.Pixels[y * imageWidth + x] = (int)colorValue;
                    }
                }

            writeableBitmap.Invalidate();
            btImage.Source = writeableBitmap;
            GC.Collect();
        }

To view the full code download the project below.

Silverlight 3 is required

demodownload

6 Responses to “Silverlight 3 Writeable Bitmap API - Plasma”

  1. zproxy says:

    You might also be interested in the Mandelbrot Example:
    http://zproxy.wordpress.com/2009/05/01/mandelbrot/

  2. Kris Meeusen says:

    Yeah I’m not really into mandelbrots
    It reminds me of school ;)

  3. [...] Pueden visualizar y descargar la aplicación aquí. [...]

  4. [...] Silverlight 3 Writeable Bitmap API - Plasma « lab101 http://www.lab101.be/2009/06/silverlight-3-writeable-bitmap-api-plasma – view page – cached Inspired by old school MS DOS demos and this Flash Alchemy experiment. Found out that someone already ported some code from the Alchemy experiment to C#. — From the page [...]

  5. [...] Kris Meeusen used WriteableBitmap to generate various kind of graphic pattern using some simple Mathematics. He used multithread to speed up the calculations. [...]

  6. Deon Licause says:

    Sick and tired of getting low amounts of useless traffic to your site? Well i want to let you know about a fresh underground tactic that produces myself $900 each day on 100% AUTOPILOT. I possibly could be here all day and going into detail but why dont you simply check their website out? There is really a excellent video that explains everything. So if your seriously interested in producing easy hard cash this is the site for you. Auto Traffic Avalanche

Leave a Reply


lab101 is proudly powered by WordPress
Content and design by Kris Meeusen
Entries (RSS) and Comments (RSS).