Monthly archives: November 2009
I had a need to directly manipulate the bytes in a bitmap for my VB.Net app for Windows Phone. I found quite a few articles with The Google on how to do this in C#, using the unsafe keyword. That's a perfectly fine way of doing it for C# users. However, VB.Net does not have anything like the unsafe keyword, so there is no way to run unsafe (unmanaged) code blocks. Philosophically, this might be a "good" thing to Managed Code purists. I'm actually neutral on the issue. Still, just because you can't do arbitrary pointer and memory manipulation in VB.Net does not mean you can't do this very simple thing (dimming the color brightness of each byte of a bitmap). It's pretty easy and I can't believe nobody out there has posted how to do it this way VB.Net or C#. I came up with this code for the Compact Framework for Windows Phone specifically, but it will also work in the big Framework for big Windows. It will work in both VB.Net and C# just fine.
The key to this method is using the Marshal.Copy method. This copies a block of memory from an IntPtr to various types of arrays and then back again. Whereas the unsafe method directly manipulates the bytes in the bitmap, this code block copies the unmanaged bytes to an array, manipulates them, then copies them back to the unmanaged buffer. Using the 'unsafe' method is likely a hair faster, but in real world performance metrics, even on Windows Phone you would be hard pressed to see any noticeable difference between the two. Copying blocks of memory is a very fast operation.
This code adjusts the brightness of a bitmap, dimming it by half (a bit shift of >>1 for each color component of each pixel). Windows Phone natively uses 16bit bitmaps ('565' format, as in RRRRRGGGGGGBBBBB). The below code block can be adjusted to work with 24 and 32 bit bitmaps as needed. It's actually easier to use 24 and 32 bit bitmaps as you just manipulate the brightness of individual Byte primitives without needing to break apart the Int16 (short) using bit masks, as below. However, the act of converting the 16 bit bitmap to a 32 bit bitmap on Windows Phone seems to cause a significant a performance hit! So don't do that on Windows Phone. Stay with the 16-bit bitmap and use bit masks, as I did below. For big Windows, you will want to use 32 bit bitmaps, not 16 bit bitmaps.
Private Sub DimBitmapByHalf(ByRef bmp As Bitmap)
Dim bmd As Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format16bppRgb565)
Dim bufLen As Integer = (bmd.Stride * bmd.Height) / 2 ' Because bmd.Stride and bmd.Height are in units of 8-bit bytes
Dim buf(bufLen - 1) As Short
Dim r As Short
Dim g As Short
Dim b As Short
System.Runtime.InteropServices.Marshal.Copy(bmd.Scan0, buf, 0, bufLen)
For i = 0 To bufLen - 1
' RRRRR GGGGGG BBBBB
r = (buf(i) >> 1) And &H7800 ' 01111 000000 00000
g = (buf(i) >> 1) And &H3E0 ' 00000 011111 00000
b = (buf(i) >> 1) And &HF ' 00000 000000 01111
buf(i) = r Or g Or b
System.Runtime.InteropServices.Marshal.Copy(buf, 0, bmd.Scan0, bufLen)
I love science! NASA has confirmed that the projectile launched into the moon's surface that offered a less than Hollywood spectacular boom uncovered 24 gallons of water. That is so awesome! Water on the moon has many benefits. First, astronauts can drink it. They can also breath the oxygen, once the water molecule is broken apart. Finally, the hydrogen can be used for rocket fuel. Since all these essential resources do not need to be taken to the hypothetical future moon base, a ton of money can be saved!
But with every bit of good news, seems to come a lot of bad, these days. NASA's Ares rocket system (part of the Constellation program) is apparently over budget and the Obama administration is trying to figure out what to do about this. This is sad. After health care and jobs, science is a top priority in my book. And highly visible, highly inspirational science like the space program should be a huge priority.
Please write the president and tell them how important the space program is to human kind. Some good talking points would include the number of jobs created by the Ares program and potential future moon missions, the inspiration to the next generation of kids to get interested in science, and the importance of the USA becoming the space leader again.
The money needed is a drop in the bucket compared to the perpetual needless wars we are involved in. Only three billion more dollars is needed each year. Compare that to the nearly one trillion dollars spent so far on killing people in the middle east.
If we don't lead the world on space exploration, it appears China will be glad to fill that role.
If you tried to visit the Avian Waves website in the last 24 hours, you may have noticed that a spam page (with an embedded virus download) came up instead. My site, itself, was not hacked. No, it was much worse. The DNS provider at Enom (the company the registrar I use, Arbor Domains, is a reseller for) was hacked. A very large number of sites had their DNS redirected.
Time to switch to GoDaddy. They are cheaper anyway. Enom has proven they cannot be trusted with security. Sorry, Arbor. It's been a great ten plus year run. But your DNS and registration tools haven't been updated since the early 2000s. Plus, I need DNS service I can trust to not be hacked. And if it is hacked, it needs to be fixed ASAP.
Wow! This is shaping up to be a good season for teevee science fiction! First, we get a remade V (which didn't disappoint, although the first episode wasn't as strong as the original, IMHO), now I see that The Prisoner (an absolutely iconic science fiction series from the 60s) is being remade for AMC. Go AMC! Go teevee!
For several years now, BP has subjected us to the endless "common sense" of its scripted sock puppets on the teevee machine. Now the Natural Gas lobby has started the same type of ad campaign. Did you know we have over 100 years of Natural Gas?! ZOMG BURN IT ALL UP NOW BABY! SCREW THE ENVIRONMENT!
For crying out loud. Give me Solar, damn it. Screw your fossil fuels.
I am a big fan of the original mini-series, V, that aired in 1983. I was pretty young back then, so my recollection of the series is primarily through re-runs on Syfy years ago before the channel had a stupid name. The mini-series was gripping and original. The WWII analogies were strong and well written, and the character reactions of the story were very plausible. In addition, the main protagonist was a cameraman. Give it up for the geeks! The story ended with a victorious battle, but the war was very far from over. There were lots of open ended questions and cliff hangers.
The next year, a sequel mini-series was made, V: The Final Battle. It completely sucked1. They either ignored or quickly wrapped up the very dramatically laid loose ends from the previous year in very unsatisfactory ways. The story devolved from a WWII analogy into more of a religious analogy, with the hybrid human-visitor having magical Christ-like powers that neither parent possessed (seriously, wtf?). And they had Michael Ironside. (Has your favorite show jumped the shark? If Michael Ironside was just added to the cast then the answer is yes!) And to top it all off, the humans beat the visitors with the most lame War of the Worlds deus ex machina2 rip-off ending ever – complete with like fifteen minutes of stock footage cheering and rejoicing! After that, NBC started a series sequel to V. The visitors quickly developed an immunity to the poison dust, lost their warbled distorted voices (no explanation given), regained rule over the planet with little resistance, and the series continued to suck and disappoint like only a series that jumps the shark before it even begins can!
Anyway, so there's a new V series starting tomorrow. I'm very excited about it. The whole premise of V is very rich and exciting. It's unique in that the aliens don't just nuke cities a la Independence Day, but instead use politics, torture, mind control, and media manipulation to gain control of Earth. I think the WWII parallels also create a very eerie sense of "omg, dude, this could totally really actually happen" since in human history it did actually happen (sans aliens) in Germany.
The one thing that concerns me about the new V series is that the synopsis sounds more like the reimagined Battlestar Galactica than the old V. There is plenty of room for the writers to come up with great storylines, don't get me wrong, I just hope that it does the original honor with a lot of manipulation and very little nuking. Battlestar already thoroughly explored the human condition (IN SPACE) with regard to modern problems (terrorism, modern warfare, torture, etc.). I want a strong manipulative invasion story, where humans are pitted against humans and the visitors are pulling the strings. Here's hoping it doesn't suck! (Reviews are good so far, so I'm excited!)
1. The Final Battle was apparently made without much input from original "V" mythos creator, Kenneth Johnson. Mr. Johnson eventually published a book in 2008 which contained (I would presume) his preferred ending to the V story, ignoring the entire Final Battle and subsequent series storylines completely. I need to read this.
2. I should point out that War of the Worlds is not lame, despite the lameness of the similar ripped-off ending used in V: The Final Battle.