Resizing PuTTY

So today in my down time at work, I thought it would be useful to make a ’session manager’ for PuTTY windows.  There are situations where it is necessary to ssh into 15-30 servers, where scripting the task isn’t practical.  Setting up these windows always take A LONG time.  Then if something else happens, you have to close them all out, setup the windows…blah blah.  Anyway, free time is free time.

So, I turn to vba again (i really don’t like the environment, but currently hands are tied).   Sounds easy enough.  Who doesn’t test their window movements and resizing with notepad.  I know I do.  Everything worked fine.  Then I try it with PuTTY.  Movement is fine, resizing is not.  Since I’m forced to write in VBA, you can probably guess that I didn’t have Spy++ at my disposal (which was definitely too bad).  Anyway, I was forced to download the source code and plunge through the main window procedure to find the handled resize events.  Turns out, i had to use WM_ENTERSIZEMOVE and WM_EXITSIZEMOVE to surround my resizing messages.  Notepad, and most other applications don’t require this.  The reason PuTTY requires it is because they resize based on rows and columns (there are other options, but seem ridiculous).  Regardless, these two messages weren’t the first place in their code I jumped to, and it took way to long for me to figure that one out.


Call SendMessage(hwnd,WM_ENTERSIZEMOVE,0,0)
rst = SetWindowPos(hwnd,...)
Call SendMessage(hwnd,WM_EXITSIZEMOVE,0,0)
Just to prove a previous point

Just to prove a previous point

So I’m guessing that I should start wrapping my resizing and movement messages inside these guys.  When you watch it in Spy++, they always get sent.  You never know when an application might actually need them.

Leave a Comment

smack

Earlier today I was writting some vba code (since it was all that was readily available on the computer that had easy access to retrieving hwnds).

-Consequently, later in the day (in perl mode) i pulled one of these moves:

my @_rst = grep {$_ = $_check} @_previous

Took a bit of debugging to realize that all the items in previous mysteriously became whatever the most recent value of check was.  Ugh, stupid.

Leave a Comment

Functional Desire

While processing some images, I realized how often I was repeating many of the same things for every operation. There were several lines required to loop through the image and ignore the background pixels. I couldn’t help but ask myself why am I duplicating this code? I’m staring at the only relevant lines of code (sometimes only 1 or two lines) wrapped by these loops and guards.

Allow me to start with an extremely contrived example:


    const unsigned int THRES = 120;
    const int wd = image->GetWidth();
    const int ht = image->GetHeight();
    for(int x=0; x < wd;++x){
        for(y = 0; y < ht; ++y){
            COLORREF px = image->GetPixel(x,y);
            if(! IS_BACKGROUND(px) ){
                     //process the pixel ( distinct section)
            }
         }
     }

Obviously, I am not offended by seven lines for the loop. Since most of my methods are tied into event handlers from the GUI, in order to avoid multiple passes over the image, many of the same operations are tied into that loop. This example should suffice to make my point.

Say that I want to simply convert all white pixels (considered background) to red. I could define the loop structure a method which takes and executes a functor. This method is named iterate_Image below. I created a simple iterator method for the CImage class (in poor form since I’m not really using the CImage class in my project).


#include <algorithm>
#include <functional>

namespace ImProc
{
  template<class Function>
  Function iterate_Image(CImageIterator* first, CImageIterator* last, Function f)
  {
	  while ( *first != *last){
		  f(first,first->GetPixel());
		  (*first)++;
	  }
    return f;
  }
}

/*--Process Pixel method--*/
void procPixel(CImageIterator* cit, COLORREF px){
    if(IS_WHITE(px)/*simple macro*/){
        cit->SetPixel(0x0000ff);
    }
}

/*--quick testing method--*/
void CImageViewerView::OnDraw(CDC* pDC)
{
	using namespace std::tr1;

	CImageViewerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc || (pDoc->GetPathName() == ""))return;

	shared_ptr<CImage> img = pDoc->GetImage();  /*testing in OnDraw method, but..ya, don't do that*/
	CImageIterator* it= new CImageIterator(img);
	function<void(CImageIterator*,COLORREF)> fct = procPixel; //store in function wrapper (from tr1)
	ImProc::iterate_Image(it,it->end(),fct /*or simply &procPixel*/);

	img->Draw(pDC->m_hDC,0,0);
	delete it;
}

Thats tolerable, but being forced to jump to the definition of procPixel is not particularly desirable. I wanted to write it as lambda expression. I naturally pulled the lambda’s out of the boost/tr1 library implementation. Syntactically, it wasn’t exactly what I wanted, but worked easily for simple things. I had just enough exposure to FC++ [ http://www.cc.gatech.edu/~yannis/fc++/boostpaper/fcpp.html ] to know that it existed. Anyway, I encourage you to read comparisons from other sources. The C++0x implementation looks much more promising…

Let me show you the updated OnDraw method using lambdas from FC++.


void CImageViewerView::OnDraw(CDC* pDC)
{
    using namespace boost::fcpp;
    using namespace std::tr1;

    CImageViewerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc || (pDoc->GetPathName() == ""))
        return;

    shared_ptr<CImage> img = pDoc->GetImage();  /*testing in OnDraw method, but..ya, don't do that*/
    CImageIterator* it= new CImageIterator(img);

    lambda_var<1> IT;
    lambda_var<2> PX;

    ImProc::iterate_Image(it,it->end(),
        lambda(IT,PX)[
            IT %ptr_to_fun(&CImageIterator::SetPixel)%
                if0[
                    PX %equal% ((COLORREF)0x000000),
                    0x0000ff,    //true
                    PX    //false
                ]
        ]
    );

    img->Draw(pDC->m_hDC,0,0);
    delete it;
}

You’ll notice that I had to got rid of my macro call to show a typical comparison. The one thing I noticed, is that in some cases you have to be extremely careful with the types. Failing to do so might lead to some cryptic compiler errors. I am using the infix notation to improve the flow of the embedded lambda syntax. Imagine the boolean expression was written as equal[ PX ][ (COLORREF)0x000000 ]. Similarly, the SetPixel method used is defined as follows (as you’d imagine).


void SetPixel(COLORREF)const;

As we all got exposure to from extension methods (.net dudes anyway =P), all this class junk just implies the object variable as the first argument to the method. So imagine instead that the definition were more like the following.


void SetPixel(CImageIterator*,COLORREF) const;

In case you didn’t before, you can see how the call to the member function works. That infix syntax also helps the call look more familiar. If we extend this out we can get another argument with currying. Unfortunately, it seems to only support 3 arguments, hence the need for an iterator class. <tangent>Of course, in the case of the CImage class, I only really need the void* of the pixel, and it would be more performant..blah blah. </tangent>

Now back to the original point. If you’re wondering why you would want to learn a load of syntax, heres my unqualified rational. If we had a function that handles converting these white pixels to red, called ConvertWhiteToRed (clever?), and wanted to reuse all that loop logic, the benefits are a bit more apparent. First, you are reusing code, which is always a plus (please continue to picture more filters and things going on in than in my loop examples). Second, we get rid of the indirection we would have just passing a functor. The code inside ConvertWhiteToRed actually converts the white pixels to red! Third, the syntax enabled by FC++ makes reading this code fairly reasonable. In my example, I laid out the conditional statement into a block so you can see how embedding lambdas might read for more in depth operations. There is also support for monads in the library.

Ya, so all that was a fun but unnecessary divergence from my project. If you want to play with lambdas etc within imperative languages, C# provides a more natural feel (since its built in?..maybe…hint,hint =P). I realize this was a simple example, and created quickly…so if you find mistakes please kindly correct them. Next up, working with contracts (Eiffel anyone?). I just watched a video on spec# and it seems like it would be worth my time to look at for a bit. Check it out at Channel9 (along with all the other awesome videos there) http://beta.channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Contract-Oriented-Programming-and-Spec/ . Then back to the project at some point.

Leave a Comment

Current ‘Project’

From time to time I take a concept in software and try to apply it project to force my learning. Most of the time these project remain idle and there is really no intent to finish them. Right now my focus is on computer vision. I recently finished a music theory class where our final project was to compose a piece of music. To create the score I first wrote ideas on paper (mostly on my tablet in journal), transcribed them into a easily editable midi format, and eventually to lilypond for engraving. I thought it would be a worthwhile project to create a tablet application that would attempt to recognize a handwritten score, and write it directly to a lilypond backed format. This would give me the ability to quickly test a measure or phrase of written music directly in a quickly editable handwritten format.

I am writing the front end with the VC++ feature pack 2008. So far I am very impressed by everything that’s in there. Check it out at http://www.microsoft.com/downloads/details.aspx?
FamilyId=D466226B-8DAB-445F-A7B4-448B326C48E7&displaylang=en
.

As I mentioned, I don’t expect this to be a project that will ever be completed, but I am interested to see how reasonable an application like this is. I will post updates as I complete certain portions of the app.

I have done next to zero research on the idea, so it is very possible that something like this already exists. Maybe someone will steal the idea, then I can get the program without the work =). Either way, I am only using it as a learning experience.

Leave a Comment

First Post

Hello,

I started this blog so that I can post about topics that my friends don’t want to listen to. Updates may be scarce. We’ll see how it goes. Most topics will revolve around software. Right now I am spending time in the c++ world, but do jump around in the managed world from time to time. I am no expert in either, so my opinions don’t mean a whole lot.

Leave a Comment