Latest Posts

.

The Perspective “we” all miss…

{ Posted on 9:32 PM by Vivek }
Tags :

 

This is my second purely non technical post since my first one.  I like writing applications and also using them. But I as a .. ahem.. developer, miss being “the user”. What I mean is that I completely forgot the “user only perspective”. Think about facebook or orkut. If you’re a normal user, you’d think about “doing what they were meant for” messaging and scraping and chatting and all the good stuff, but if you’re developer you would wander off into a dream that only a software geek understands. As for me, I’d think of the AJAX UI, JavaScript, I do use them, but the essence of wonder, the surprise is spoiled. Much like a magic trick, If you know how its done you’ll loose the element of bewilderment (going insane and saying ”its freaking awesome” ).

I think when it comes to software, that is one of the things that drives a developer (IMHO of course), i.e we developers don't stop at saying “who cares as long  how it works ”, we do care. Well I don’t mean to say that Non-developers are ignorant its just that they don’t fit in very well. Again I mean no offense to the non-developer community. Its like I wondering how a particle accelerator like CERN work. I mean the wonder and  the surprise I have hearing about it stays that way, one day if I know a respectable amount (though very very unlikely) of information about the CERN, may be i wouldn't be so awed at hearing facts those mind blowing facts about it.

I just cant imagine the sense of amazement i would have if i had no clue about how Microsoft-Touch works and I get to use it for the first time.Hmm. Like I said I don’t know. You have to be a complete user to understand it. Oh i wish I could.

At the same time imagine you’re with a non-developer friend and you’re Burning a CD, as the progress bar, progresses (pun intended), suddenly he cant stand it any more and he tells you “why don’t they make it faster by allowing us to drag the progress bar”, thinking about slide controls or scroll bars. Now those are the kind of situations where you feel a tad more grateful for being a developer. :)

Another instance is where another friend of mine got excited, and asked me if i can write a program for quickly charging his laptop. They’re not being ignorant they’re used to “free lunches” like that. Its our fault.

Either way its a different deal being a developer, I love it absolutely but the thing is this mysterious longing to be a complete computer novice   again. May be I’m being a little spiritual here, but hey,I’m being honest.Personally I think being a developer is being a magician (a conjurer if you’re British). Fellow magicians can sometimes surprise each other, but they usually work out the mechanics. But  a user in many ways are like the spectators. They simply have more fun.

I definitely miss those days, when using MS paint was a trip to Disney land,

as my uncle once commented on seeing his son Alex going wild painting on MS Paint.

”hey, don’t use up every bit (of paint ) leave some for tomorrow”

Developing an Application, with C# , a bag of chips..and .....a Profiler !(updated)

{ Posted on 11:54 PM by Vivek }
Tags :

My previous post was about a solution to Bloaty Digi-cam photos, since the solution itself was a little tiresome to carry out manually, and there seemed to be no way to automate the process, I decided to do some good in the world, by writing a small application that would do that. Folks I a newbie to software development, and I know how amateurish the whole thing looks, as i write more and more code the more experienced I become,much like a pilot, the more he flies, the more he gains. But not every minute of his flying counts, there will be some tumbling and fumbling in the beginning. The same (I believe) applies to coding, not merely the lines of code,but  the quality of it. You’ll soon see where all this corny talk of mine leads to.

My delusion on the “Apparent success”?
How long did it take to write this app?

Well, an afternoon, by my standards that’s remarkable(actually remarkably deluded), I said to my self, but as my application went to the testing department, everything went down the trenches. The rest of this post is my interesting journey… “Developing an Application, with C# and a bag of chips..and .....Oh a Profiler”

As usual I fired up visual studio, I not a Microsoft shil, but I’ve got to say that in my book there’s no software development without VS.

Then I phrased the problem in my mind, I need to somehow replicate whatever MS Paint is  doing to reduce the size of the images. You’ll find it weird as I did, when you’ve read my previous post. Because effectively what paint was doing we just saving the JPEG to JPEG, no format conversion, re-coding , no rescaling , but it did change its resolution, but only a little.

So in order it achieve that I just have to Load the JPEG image and save it to a file stream again. Seems simple enough.

For those of u from Java background, in .net there are a lot of Nifty classes to play around with Images, using which i can save to and read from many formats Jpg,Bmp,tiff, etc..,The problem was it didn't quite have the effect of MS paint. The file size was still sky-high. So I remembered about the FreeImage library, which i thankfully heard about a year ago. I forgot all the documentation, but it was easy the only problem was it was an unmanaged (ie non- .net ) library, so there is a magic called P/Invoke involved in using a non-managed library, from a managed framework. I know how java programmers feel about this, but dont forget there are a lot of performance incentives involved with a little pain incurred, by writing a wrapper to the non-managed libraries such as FreeImage. Any way I ended up writing a C# wrapper to two important function i needed from the massive 2MB library.

They were

[DllImport("FreeImage.dll")]
public static extern int FreeImage_Load(int format, string filename, int flags);
[DllImport("FreeImage.dll")]
public static extern bool FreeImage_Save(int format, int handle, string filename, int flags);




the parameters are self-explanatory, int format is the file format for Jpeg it was 2,

 



I implemented these things with the following function




public void SaveToJpegOptimized(string UnOptimizedFile,string OptimizedFile)
{
try
{
int a;
a = FreeImage_Load(FIF.FIF_JPEG, UnOptimizedFile, 0);
FreeImage_Save(FIF.FIF_JPEG, a, OptimizedFile, 0);

}
catch(Exception exp)
{
System.Windows.Forms.MessageBox.Show(@"Houston we have a problem " + exp.Message.ToString () );

}
}









Actually there was another function which the application critically needed. More on that later.



Apparently All I had to do was Load the Jpeg and save it.So the logic was sketched. Now coding time…



Some people are religious about whether they should design the UI and code or backwards. I figured out it was better to Design the UI since the application logic was pretty much completed.



so here it is. the  completed UI



image



I could’ve done this a little professionally, but i wanted it to be intuitive and not intimidating for non-geek users. Since were talking about talking to the Hard Disk (pun intended), I decided to go for Asynchronous operation, and It paid off. Especially when the user chooses to operate on multiple files. And with the Minimize to tray option the app sits quietly and does the job on the system tray.



Everything was going great until i decided to put the app to the test.



I mercilessly made it to chew on two hundred files







image



The application before eating the 23rd file, drank  440MB of Precious Memory.



I smelled MEMORY LEAK….



It was time to call the super hero at times of such peril.



TADA the Profiler, a blessing to the developer,



It quickly showed me where I went wrong Of course an experienced developer would a found out the cause in a snap,but it proved difficult for me.



The cause of the application to consume that much memory was that i forgot to call an important function.




[DllImport("FreeImage.dll")]
public static extern void FreeImage_Unload(int handle);






The FreeImage_Unload(int handle) de-allocates the memory needed to hold the image in memory.




private void Folderworkermethod()
{
try
{
string[] files = Directory.GetFiles(textBox2.Text, "*.jpg", SearchOption.TopDirectoryOnly);
DirectoryInfo drin = new DirectoryInfo(textBox2.Text);

FileInfo fileinfo;
progressBar2.Value = 0;
foreach (var file in files)
{
fileinfo = new FileInfo(file);
oldfoldersize += fileinfo.Length;
}
progressBar2.Maximum = files.Length;
drin = null;

oldfoldersize = oldfoldersize / 1024;
lblfoldersize.Text = oldfoldersize.ToString() + "KB";
string tname = "";
this.img = new ImageUtil();
progressBar2.Step = 10;
int filecnt = 0;

foreach (var file in files)
{

filecnt++;
tname = file + ".temp";
if (File.Exists(file + ".temp"))
File.Delete(file + ".temp");


File.Move(file, tname);


img.SaveToJpegOptimized(tname, file);
System.IO.File.Delete(tname);
tname = "";
progressBar2.Value = progressBar2.Value + 1;
lblfilecnt.Text = "File " + filecnt + " of " + files.Length.ToString();

}

foreach (var file in files)
{
fileinfo = new FileInfo(file);
newfoldersize += fileinfo.Length;
}
newfoldersize = newfoldersize / 1024;
lblfolderafter.Text = newfoldersize.ToString() + "KB";
lblfoldersizesav.Text = (oldfoldersize - newfoldersize).ToString();
img = null;
files = null;
}
catch (Exception)
{


}

}







At line number 35 you can see that after a file has been processed the memory can be reused, which the application failed to do.



The code




public void SaveToJpegOptimized(string UnOptimizedFile,string OptimizedFile)
{
try
{
int a;
a = FreeImage_Load(5, UnOptimizedFile, 0);
FreeImage_Save(5, a, OptimizedFile, 0);

}
catch(Exception exp)
{
System.Windows.Forms.MessageBox.Show(@"Houston we have a problem " + exp.Message.ToString () );

}
}





 


Became




public void SaveToJpegOptimized(string UnOptimizedFile,string OptimizedFile)
{
try
{
int a;
a = FreeImage_Load(5,UnOptimizedFile, 0);
FreeImage_Save(5, a, OptimizedFile, 0);
FreeImage_Unload(a);

}
catch(Exception exp)
{
System.Windows.Forms.MessageBox.Show(@"Houston we have a problem " + exp.Message.ToString () );

}
}





That’s right all it took was a single line extra. 8



Folks this is the side effect of managed programming The GC cannot interfere with the unmanaged code region, in C++ as we all know it is left to the coder to dispose all the objects with the( long forgotten )Destructor.(the nostalgic memories may return to haunt you :) . Any way That was it instant cold relief, I mean instant relief from Memory leak.I prescribe a good dose of profiling and experimentation.



Hope the boredom didn't kill you :) .



Until the next adventure



Vivek

A Solution to Bloaty Digi- cam Photos

{ Posted on 6:19 PM by Vivek }

I remember once, when I had to scan 10 or so certificates and photos, and had to E-mail them. As i was uploading them i hit the ceiling of 10 MB quite soon. About the third or fourth picture. I wondered why and as i saw the size they around 2-3 MB . Easily 4 Photos would’ve been enough to reach the 10MB limit. Unfortunately I couldn't “Google” for the lack of time. Then I sort of opened one of pictures with paint (accidentally by clicking “Edit” instead of “Open” in the Explorer context menu). Then I thought I would save the picture to another folder, that would be like copying the same file to another folder.Ok. Not quite. Something interesting happened. The file size was reduced by 70 %. I didn't notice this at first. But I did eventually. Then I repeated it again, with another pic. And ended with a smaller file size.

You dont have to take my word for it

see for yourself

1.98 MB 2592 X 1944

Bloaty Picture

big

523 KB 2592 X 1944


Same picture, Smaller Foot Print

small

That’s roughly 75 % saved without any noticeable difference.

Well if you are a picture lover and have a lot of photos taken with Digi-Cams. You’ll find this, easy on the Hard disks. Ahem.. a little math. Say you have 100 photos with a average file size of 2MB per photo you need .about…? that's right 200MB how much is 75% of 200MB .150MB saved. If you have 1000 photos with 2M average you can save 1.5 GB.Note that I’m being pessimistic, actual results may be greater. On the other hand some times the size may actually increase, a little bit ( 5 – 10 KB).



.

There’s just one catch, you can’t automate (at least as far as i know), paint to do this. So i decided to write a tiny app. That’ll do just that. Actually it doesn't use paint. It uses the infamous FreeImage library.

The following is about the Nitty –Gritty details of the application itself. If you dont like the sound of it just

download the Application here

I just call it the Jpeg Optimizer


Also note: Even though the application does not rescale the image, it does change its resolution but only a little, no noticeable difference unless you choose to zoom 200-500X.
The application does not feature a "backup the images" facility( for now).


Disclaimer:

The software is provided as is, without any warranty. I cannot in any way guarantee the correct functionality of the software. The software is free to use copy an distribute as long as it is intact.

Copyright Notices:

FreeImage Is an open source library maintained by Hervé.Under the GPL license.

Background

The application is built on the .net framework, in C# language. I intentionally decided to target the 2.0 Framework, since many people dont have .net 3.5 installed. Besides I had everything to get going in .net 2.0.The app is multi-threaded for a responsive UI.

More about the application on the next post.

Weird Yahoo Server Response..

{ Posted on 3:04 PM by Vivek }

I seriously dont mean to be off-putting,but this post contains Extremely Boring Geek content..

I should’ve probably tweeted this,its a bit large , to be considered as a micro blog.Any way i was checking my email the other day, i happen to use yahoo among other mailers. As I was about to clear my spam folder , the whacky AJAX UI , did something weird .

Take a look at this,

No its not the regular 404 ”not found” its says 502 “Cannot Find server Or DNS Error”.

Also it gives me instructions to go to the Help -> About. ok…then again where can i find About Internet Explorer in Firefox.

Its either something’s going on there between,Yahoo and Microsoft or Yahoo’s server just assumed that everyone would probably use Internet Explorer (which is so untrue).

But is it Yahoo’s fault,if not, I have a hard time imagining Firefox doing that. Cause Firefox uses Gecko Engine, Microsoft uses well …How the heck do I know). I’d put my money on Yahoo doing, that cause , isn’t it the servers responsibility to handle these kind of errors..

Feel free to enlighten me dear readers..

If you feel like, “Big Deal”, after reading this

I warned u…:)

I have a bunch of freaky, bugs, errors or goof-ups, just like this one..I’ll post them soon.

Let there be Blog !

{ Posted on 9:56 PM by Vivek }

My first..err.. encounter, (if you will) with the word “blog”, was a year or two ago. And it took me a month to “understand” that it was just a fancy word for writing articles (in the cloud).

I also realized, that just because i’m  following technical blogs, doesn't mean Blogs were meant for technical stuff. There are a lot of non-technical yet great blogs. I admit that , initially i found it outrageous,and whenever i ran into a blog I was like “who would want to read a monologue of someone, I'd rather spend the time doing something else”. I changed that opinion soon enough.

But some blogs that i read are highly biased,and that's not how articles are supposed to be. (or do they). Some are really worthwhile like

this one by my cousin brother. and this one by my cousin sister,

(I find innovative ways to flatter people)

Some general , and some blogs exists just to rant their Ex-Employers Here’s one GUY. and here’s the proof that he is so. listen to his podcast,

And boy, is he obnoxious, if i knew English better i would‘ve found a better word for this guy, sheesh , makes me wanna punch him.

Either ways it seems  to me that “blogs are supposed to reflect what you think and feel about what you actually want to blog in the first place“

(feel free to comment, critique or rant about this statement made by “yours truly”)

Er..I know..I feel like an idiot ..because I'm blogging about blogging.

(C’mon folks, its my first blog so cut me some slack..)

Anyway , i have decided to give it a try myself. And viola here it is.

Disclaimer: if you are offended , or you have a bad-hair day or feel dizzy  after reading this I’m not responsible.. Kidding..thought it’d be cool to have a disclaimer.

And being a self-proclaimed geek, my subsequent blogs will be considerably, geeky occasionally General. I'll definitely find a balance. Any way dear reader Keep reading.

bye for now.

Tweet Me at:@vivekbernard

E-Mail me @ vivek[dot]bluestar[at]gmail.com