Page 1 of 8 in the Programming category Next Page
# Thursday, July 22, 2010

This post is used to test Facebook's fb:like widget after I done some editing to the system.

Ok.. I give up, this whole thing just works REALLY REALLY weird, I can only suggest that if you want to use the fb:like button then do remember to put the Open Graph tags.


Thursday, July 22, 2010 10:15:48 PM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Wednesday, July 14, 2010

Ok, I've nabbed the new Beta developer tools for Windows Phone 7 (and still cursing the fact that no offline installer is available) dropped it into a test VHD and did a quick test project to test out wheter the previous Silverlight 4 port of the library works on the phone (and also in Silverlight 3) and... well.. it works without a hitch, so if you were looking for a QRCode library for use in  Windows Phone 7.

Download it here.

Enjoy!


Wednesday, July 14, 2010 10:59:24 PM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Friday, June 25, 2010

Google has a great barcode reading program/library in the form of ZXing. They had a CSharp branch already but not a Silverlight one. So I looked through the code a little and ported the QRCode portion of the library for Silverlight use by doing the following

  • Replaced System.Collections.Arraylist with System.Collections.Generic.List <Object>
  • Replaced System.Collections.HashTable with System.Collections.Generic.Dictionary <Object,Object>
  • Removed all Serializable attribute markings
  • Forced all calls to System.Text.Encoding to use UTF-8 as the encoding type.

While the first two changes might have a performance impact to the code, it's the LAST change that worries me since it deals with encoding, specifically I worry about exactly how well can the code deal with NON English content... oh well, that's a challenge for another day I guess.

What is that you say? Talk is cheap.. then GIVE IT A TRY!! How does it work? Create some tags.. save the tags as PNGs, then Read some tags by loading images which contains tags. Or.... Take a PICTURE of a tag using your webcam! Oh.. if your webcam can't take nice sharp images, you might want to invest in an autofocusing camera like the Microsoft LifeCam HD-5000.

DSC09111

What's that you say? It's an Open Source project therefore I need to contribute my code back into the trunk? Well.. sure... the source for the sample along with the lib is here.

The code was compiled for Silverlight 4, technically I don't *think* I used anything that won't allow it to work with Silverlight 3 though.

As to how to actually contribute it back in the trunk errr.. I guess I'll have to get in touch with the guys that's handling the project.

So.. 2 things that need to be done now.

  • See how to integrate with RawVideoStream and read tags off a LIVE video stream
  • See if the library works with Windows Phone 7

The 2nd would be a bit more difficult since I DON'T HAVE A WINDOWS PHONE 7 device! :P


Friday, June 25, 2010 12:03:57 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Monday, May 03, 2010

So after Facebook comment integration. Now I've got Twitter integration. At least I hope I have. This is going to be the post I test it on!


Monday, May 03, 2010 6:32:41 PM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Sunday, May 02, 2010

Is that sometimes they don't want to show up. I have to refresh the page to make them appear. Right now with our internet being shot to hell it's hard for me to tell if it's the line or problem with code.


Sunday, May 02, 2010 10:15:16 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Monday, April 12, 2010

Well I'm done packing and ready to head out the the Malaysian launch of Visual Studio 2010 tomorrow, and if you can't make it there physically, you can still watch a LIVE webcast of the event if your office Internet holds up at the local event site.

I'm done packing, and I'm bringing a little friend for a little trial run.

DSC06419

And of course, because a hacked up little cage containing a SLAB and a bunch of velcro straps would probably scare the bejesus out of everyone.

DSC06418

Now I just like look like some dumb schmuck who is hanging a small bag from the probably fragile and not meant for load bearing hook on his bag.

Seriously... I don't know if the little strap will hold or not! Not even sure how long the battery would last... heck the last time I was in Sunway Pyramid Convention Center, cell phone reception was pretty crappy! Would I even got proper signal in there this time?


Monday, April 12, 2010 12:56:38 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Wednesday, March 24, 2010

As I posted before I was looking for a bicubic resize algorithm for images to be used in Silverlight. I thought I ported the code properly from C# to VB but then yesterday when I was testing the code some more, I realized that there was a weird pattern of noise present in the picture. It looked like a square grid of sorts being overlaid onto the image. My experience told me that, that's because something screwed up when interpolating the pixel values (duh!) I didn't notice it previously because my previous test images didn't cause it to show up.

So what was I left to do? I had to port over the C# version to a lib on it's own then.

Bicubic Interpolation
/// <summary>
/// Bicubic resize algorithm suitable for use in Silverlight
/// </summary>
/// <param name="src"></param>
/// <param name="dest"></param>
/// <remarks>Original Source For This Function - http://www.codeproject.com/KB/recipes/aforge.aspx</remarks>
public static void resizeBicubic(System.Windows.Media.Imaging.WriteableBitmap src, WriteableBitmap dest)
{
    int height = src.PixelHeight;
    int width = src.PixelWidth;

    int newHeight = dest.PixelHeight;
    int newWidth = dest.PixelWidth;

    double xFactor = (double)width / newWidth;
    double yFactor = (double)height / newHeight;

    // coordinates of source points and cooefficiens
    double ox, oy, dx, dy, k1, k2;
    int ox1, oy1, ox2, oy2;
    // destination pixel values
    double r, g, b;
    // width and height decreased by 1
    int ymax = height - 1;
    int xmax = width - 1;

    int p;
    // RGB
    for (int y = 0; y < newHeight; y++)
    {
        // Y coordinates
        oy = (double)y * yFactor - 0.5f;
        oy1 = (int)oy;
        dy = oy - (double)oy1;

        for (int x = 0; x < newWidth; x++)
        {
            // X coordinates
            ox = (double)x * xFactor - 0.5f;
            ox1 = (int)ox;
            dx = ox - (double)ox1;

            // initial pixel value
            r = g = b = 0;

            for (int n = -1; n < 3; n++)
            {
                // get Y cooefficient
                k1 = BiCubicKernel(dy - (double)n);

                oy2 = oy1 + n;
                if (oy2 < 0)
                    oy2 = 0;
                if (oy2 > ymax)
                    oy2 = ymax;

                for (int m = -1; m < 3; m++)
                {
                    // get X cooefficient
                    k2 = k1 * BiCubicKernel((double)m - dx);

                    ox2 = ox1 + m;
                    if (ox2 < 0)
                        ox2 = 0;
                    if (ox2 > xmax)
                        ox2 = xmax;

                    // get pixel of original image
                   // p = src + oy2 * srcStride + ox2 * 3;
                    int srcPixel=src.Pixels[ox2+(width*oy2)];

                    r += k2 * ((srcPixel>>16) & 0xff);
                    g += k2 * ((srcPixel >>8) & 0xff);
                    b += k2 * (srcPixel & 0xff);
                }
            }

            
            dest.Pixels[x+(newWidth*y)]=(int)(0xff000000 | ((byte)r<<16) | ((byte)g<<8) |(byte) b);

        }
       
    }
}

public static double BiCubicKernel(double x)
{
    if (x > 2.0)
        return 0.0;

    double a, b, c, d;
    double xm1 = x - 1.0;
    double xp1 = x + 1.0;
    double xp2 = x + 2.0;

    a = (xp2 <= 0.0) ? 0.0 : xp2 * xp2 * xp2;
    b = (xp1 <= 0.0) ? 0.0 : xp1 * xp1 * xp1;
    c = (x <= 0.0) ? 0.0 : x * x * x;
    d = (xm1 <= 0.0) ? 0.0 : xm1 * xm1 * xm1;

    return (0.16666666666666666667 * (a - (4.0 * b) + (6.0 * c) - (4.0 * d)));
}

Pretty much a direct port of the original code since it was in C# already previously, just changed the way pixels are read and set. Same caveat applies as the previous code, no alpha premultiplication done yet.

The C# code didn't produce the weird grid like overlay pattern. So... what happened? Well my guess would have to be that I haven't properly ported the code to VB, and somewhere there's a fractional number being cast to a whole number causing some problems.

I'll have to be more careful when porting code that involves fractions in the future then.


Wednesday, March 24, 2010 10:08:14 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Friday, March 19, 2010

UPDATE : This code doesn't work properly, go here for a properly working one.

I needed to resize an image in Silverlight and then save it out as a JPEG. I could have just used a transform on it then rendered it to a WriteableBitmap but the transforms were optimized for realtime performance and doesn't work too well when you need to maintain quality.

I stumbled upon a Bicubic Resize code in a library called AForge.Net and it was in C#, so it was simple enough for me to convert it to VB for me to use it in Silverlight. And since I DIDN'T find a lot of Bicubic Interpolation .Net resize code on the net, I'm gonna just put this copy up here.

As a bonus I kept on my comments in so you guys can see what was going through my head as I was working my way through the code.

Bicubic Interpolation
''' <summary>
  ''' Bicubically resize the src bitmap to the dest bitmap
  ''' </summary>
  ''' <param name="src"></param>
  ''' <param name="dest">Destination Bitmap, should be initialized to the requested size.</param>
  ''' <remarks>Original Source For This Function - http://www.codeproject.com/KB/recipes/aforge.aspx</remarks>    
  Sub Resize(ByVal src As Imaging.WriteableBitmap, ByVal dest As Imaging.WriteableBitmap)

      Dim startTime As DateTime = Now


      Dim srcwidth As Integer = src.PixelWidth
      Dim srcHeight As Integer = src.PixelHeight

      Dim destWidth As Integer = dest.PixelWidth
      Dim destHeight As Integer = dest.PixelHeight

      Dim xFactor As Double = srcwidth / destWidth
      Dim yFactor As Double = srcHeight / destHeight

      'Coordinates of src points and  coefficients.. WTF?
      Dim ox, oy, dx, dy, k1, k2 As Double
      Dim ox1, oy1, ox2, oy2 As Integer

      'new color values
      Dim r, g, b As Double

      Dim xmax As Integer = srcwidth - 1
      Dim ymax As Integer = srcHeight - 1

      For y As Integer = 0 To destHeight - 1
          'this is getting the original Y pixel if I'm not mistaken
          oy = y * yFactor - 0.5F
          'WHY is this necessary?
          oy1 = oy
          'WTF? It's getting the FRACTIONAL difference between
          'oy and oy1?
          dy = oy - oy1


          For x As Integer = 0 To destWidth - 1
              'same WTF as with the Y values,
              'maybe will make more sense later?
              ox = x * xFactor - 0.5F
              ox1 = ox
              dx = ox - ox1

              r = 0
              g = 0
              b = 0


              'this loop is to gather the interpolated
              'values of 2 pixels surrounding the current one
              For n As Integer = -1 To 2
                  'this gets the Y coefficient

                  k1 = BicubicKernel(dy - n)

                  'this seems to be getting the new
                  'Y pixel where the interpolated
                  'value comes from
                  oy2 = oy1 + n

                  'this is to ensure we're in the right spot
                  If oy2 < 0 Then
                      oy2 = 0
                  ElseIf oy2 > ymax Then
                      oy2 = ymax
                  End If

                  For m As Integer = -1 To 2
                      'for X coefficient
                      k2 = k1 * BicubicKernel(m - dx)

                      ox2 = ox1 + m

                      If ox2 < 0 Then
                          ox2 = 0
                      ElseIf ox2 > xmax Then
                          ox2 = xmax
                      End If

                      'get original pixel color
                      ' Dim origColor As Color = source.GetPixel(ox2, oy2)

                      'writablebitmap pixel is AARRGGBB
                      Dim srcColor As Integer = src.Pixels(ox2 + (srcwidth * oy2))


                      'set interpolated values
                      r += k2 * ((srcColor >> 16) And &HFF)
                      g += k2 * ((srcColor >> 8) And &HFF)
                      b += k2 * (srcColor And &HFF)
                  Next
              Next

              'after calculating coefficients we have our new color               
              dest.Pixels(x + (destWidth * y)) = &HFF000000 Or (r << 16) Or (g << 8) Or b


          Next
      Next
      dest.Invalidate()
      Dim endTime As DateTime = Now
      Diagnostics.Debug.WriteLine(String.Format("Time taken to squish {0}x{1} to {2}x{3} : {4} seconds", srcwidth, srcHeight, destWidth, destHeight, endTime.Subtract(startTime).TotalSeconds))

  End Sub

  Shared Function BicubicKernel(ByVal x As Double) As Double

      If x > 2.0 Then
          Return 0.0
      End If

      Dim a, b, c, d As Double
      Dim xm1 As Double = x - 1.0
      Dim xp1 As Double = x + 1.0
      Dim xp2 As Double = x + 2.0



      If xp2 <= 0 Then
          a = 0
      Else
          a = xp2 * xp2 * xp2
      End If

      If xp1 <= 0 Then
          b = 0
      Else
          b = xp1 * xp1 * xp1
      End If

      If x <= 0 Then
          c = 0
      Else
          c = x * x * x
      End If

      If xm1 <= 0 Then
          d = 0
      Else
          d = xm1 * xm1 * xm1
      End If
     
      Return (0.16666666666666666 * (a - (4.0 * b) + (6.0 * c) - (4.0 * d)))

  End Function

There's one caveat though, I didn't do any Alpha premultiplication with this code, which basically means if your images have a non FULLY opaque region, the results won't be pretty. Here's your homework assignment if you want to have that functionality.

I learnt 2 things while porting this code.

  1. Don't use Math.Pow() if your code is time sensitive, ie. it needs to work REALLY REALLY fast. Like in a game render loop. Takes a bit longer to run math.pow(x,3) instead of x*x*x. Not saying you shouldn't use it, just don't use it when MILISECONDS matter.
  2. Visual Basic's IIF statement isn't good for performance as well since BOTH true and false statements seem to be evaluated and does execution time is wasted when execution time matters. Better to use the usual IF..ELSE..ENDIF construct.

After my little exercise in dabbling with this, THEN I find a WriteBitmapEx library which pretty much seems to be a very useful class for raw manipulation of images in Silverlight, check it out!


Friday, March 19, 2010 12:24:20 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Thursday, February 18, 2010

So I’ve talked about Small Basic before, and they’ve just released a new version. So what’s so cool about this nice little tool for learning basic? Well it’s about how you can now show code to people. For example, let’s say I have a simple turtle program and while I can show you the code here.

start:
GraphicsWindow.Clear()
Turtle.MoveTo (0,0)
Turtle.MoveTo(GraphicsWindow.Width,0)
Turtle.MoveTo(0,GraphicsWindow.Height)
Turtle.MoveTo(GraphicsWindow.width,graphicswindow.Height)
Turtle.MoveTo(0,0)
Goto start

Then start explaining what the code does or… if you have already downloaded Small Basic, I could give you the Small Basic code of TRM169

Or… you can just SEE THE CODE IN ACTION with the brand new RUN IN SILVERLIGHT publishing capability and you can actually see how the code runs!

And that is just so farking cool! Unbelievably useful for showing people how code samples would work! Think there are still some bugs though, but not a bad start.


Thursday, February 18, 2010 8:50:55 PM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Tuesday, January 12, 2010

I was getting a few complaints that the websites we were working on stopped working on Google Chrome. Which came as a big surprise to me since when I repeated the test on my system and it worked properly. I managed to take a look at one of the users who was having problems and noticed that she was running the latest Chrome Beta with Extensions support. She had already installed a few extensions as well.

Acting on a hunch I disabled all extensions and... the website started working again.

Why was this happening? Well, the extensions/add-ons/plugins you've installed onto your browser (Doesn't matter if it's IE, Firefox or Chrome) have the capability of manipulating and analyzing the page you're viewing so they can do whatever it is they do. And when they DON'T do it properly, they start breaking the pages you're viewing by maybe.... inserting elements of their own, or changing tags and CSS class names of elements? In the age of AJAX where the webpages themselves are heavily running client side code to manipulate the document at the client end this could lead to disastrous results.

I guess the fact that extension support is technically still in Beta for Chrome also adds to the reasons for failure.

While these incidents with the extensions just reinforces my mindset that all web browsers are inherently stable until 3rd party plugins/add-ons/extensions are introduced. I worry about the day where a client will insist that there's a problem with a webpage because it doesn't work with their browser that has a particular extension/plugin/add-on installed.

So, if you had a webpage that was working fine previously before you added that new fangled Firefox Extension or Chrome Extension Or IE Add-On, maybe you might want to turn that particular thing off and try the page again?


Tuesday, January 12, 2010 10:23:29 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
Page 1 of 8 in the Programming category Next Page