Re: VB6 versus VBNet Challenge
- From: Tom Shelton <tom_shelton@xxxxxxxxxxx>
- Date: Thu, 29 Nov 2007 00:41:11 -0800 (PST)
On Nov 29, 1:25 am, "Mike Williams" <mi...@xxxxxxxxxxxxxxxxx> wrote:
"Tom Shelton" <tom_shel...@xxxxxxxxxxx> wrote in message
news:e4a21924-00ea-4e34-83d4-4e252edc6594@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Mike...
I'm not sure if Michael C will ever respond - but, I would be
happy to have a go at a VB.NET version. The problem is
I'm not a graphics guy, by any stretch of the imagination, and
so I'm not sure what you mean by inverting the bitmap?
Well I've taken it to mean flipping each bit in each data byte in the image,
so that for example any bytes with &HFFFFFFFF (white) become &H00000000
(black) and any bytes with &H00000000 (black) become &HFFFFFFFF (white), and
of course with other data bytes being "bit flipped" regardless of the data
they contain. This causes the image to look a bit like a "funny coloured
negative" and of course if you flip the flipped image you end up with the
original image you started with. Running the function any odd number of
times will end up with a flipped image (hence his requirement of 1001 calls
to the function rasther than 1000, so you can see the result). Running it
1001 times again will give you back the image you started with.
And can you give me a quick
primer of the basic algorithm?
data = Not data
Mike
Ok...
VB.NET:
Option Strict On
Option Explicit On
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Module Module1
Sub Main()
Dim bmp As New Bitmap("blizz2001-1024.bmp")
Dim sw As New Stopwatch()
sw.Start()
For i As Integer = 0 To 1000
ComplementImage(bmp)
Next i
Console.WriteLine(sw.Elapsed)
bmp.Save("blizz2001-1024 inverted.bmp")
End Sub
Private Sub ComplementImage(ByVal bmp As Bitmap)
Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0,
bmp.Width, bmp.Height), _
ImageLockMode.ReadWrite, _
bmp.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0
For i As Integer = 0 To bmpData.Height - 1
For j As Integer = 0 To bmpData.Width - 1
Dim component As Byte = Marshal.ReadByte(ptr)
Marshal.WriteByte(ptr, Not component)
ptr = New IntPtr(ptr.ToInt32() + 3)
Next
ptr = New IntPtr(ptr.ToInt32() + (bmpData.Stride -
bmpData.Width * 3))
Next
bmp.UnlockBits(bmpData)
End Sub
End Module
Release mode compile:
00:00:26.5968744
So, 26.6 seconds.
C#:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main ( string[] args )
{
Bitmap bmp = new Bitmap ( "blizz2001-1024.bmp" );
Stopwatch sw = new Stopwatch ();
sw.Start ();
for ( int i = 0; i < 1001; i++ )
{
CreateComplement ( bmp );
}
Console.WriteLine ( sw.Elapsed );
bmp.Save ( "blizz2001-1024 inverted.bmp" );
}
static void CreateComplement ( Bitmap bmp )
{
BitmapData bmpData = bmp.LockBits (
new Rectangle ( 0, 0, bmp.Width, bmp.Height ),
ImageLockMode.ReadWrite,
bmp.PixelFormat );
unsafe
{
byte* ptr = (byte*)bmpData.Scan0;
for ( int i = 0; i < bmpData.Height; i++ )
{
for ( int j = 0; j < bmpData.Width; j++ )
{
*ptr = (byte)( ~*ptr );
ptr += 3;
}
ptr += bmpData.Stride - bmpData.Width * 3;
}
}
bmp.UnlockBits ( bmpData );
}
}
}
Release mode compile:
00:00:00.7690147
Or about .8 seconds
My system - Intel Core 2 Duo E6600, 4GB Memory, Windows Vista Ultimate
32-bit.
--
Tom Shelton
.
- Follow-Ups:
- Re: VB6 versus VBNet Challenge
- From: Mike Williams
- Re: VB6 versus VBNet Challenge
- References:
- VB6 versus VBNet Challenge
- From: Mike Williams
- Re: VB6 versus VBNet Challenge
- From: Tom Shelton
- Re: VB6 versus VBNet Challenge
- From: Mike Williams
- VB6 versus VBNet Challenge
- Prev by Date: Re: VB6 versus VBNet Challenge
- Next by Date: Re: I used to use VB but...
- Previous by thread: Re: VB6 versus VBNet Challenge
- Next by thread: Re: VB6 versus VBNet Challenge
- Index(es):
Relevant Pages
|