Category Archives: Programming Examples

This category is for posts dealing with programming examples.

StumbleUpon-ed

I received 10,765 visitors yesterday, which is pretty good for a small site like this one. Late in the day yesterday I checked my stats and kind of had to do a double take. As soon as I saw the number I knew something had to be up. I checked my referrers and noticed StumbleUpon had sent me a boat load of people.

After a little investigation I found the Text Ascii Art Generator (TAAG) app had for some reason obtained a lot of “thumbs up”‘s from the StumbleUpon community. Someone added TAAG to StumbleUpon months ago, and I received a flurry of visits for a few days after that, but it topped off at around 600, nothing close to the traffic I got yesterday. I’m not sure what caused this avalanche of attention (it seems to have started in the mid afternoon), but it was pretty cool. I haven’t had that many visitors in a single day since I was mentioned in the Lockergnome News Letter back in 2000 (which at the time was a very popular online news letter – now it appears to be some kind of blogging community).

Right now it’s almost 1:30AM and as I write this, and I currently have 1.5k visitors for today already. It’ll be neat to see how long this StumbleUpon bubble lasts. So far my site appears to have held up without any problems too, so site5 earns itself a plus mark in my eyes. 10k isn’t a whole lot, but it’s good to see that sudden bursts of traffic are handled without a problem.

What I’ve Been Doing

It’s been a while since my last update. I’ve been jumping between projects, probably a little too much. Last weekend I was doing some C# stuff, and this past weekend I did a little Javascript (which I’ll talk about below). After reading all the hype on Python, part of me contemplated rewriting the Image Color Palette Generator (ICPG) in Python just as a learning exercise, but I decided to drop the idea after a little more thought. I’ve got too much going on already, and it’s probably better for me to focus on a hand full of projects than it is to try and get a taste of everything.

Ajax Image Uploading

I got image uploading to work separately from the image processing in the ICPG program. Using this nifty jQuery plug-in I was able to set the program up so the user could upload an image and then hit the processing button. It actually worked right out of the box, which is something of a rare occurrence.

ICPG used to handle image uploads, but I kept getting time out errors because my host only allows 15 seconds of processing time for each php file before it kills it. By having image uploading occur separately, I think I can get around this.

However, even though I have something that works now, I’ve decided to wait on uploading it (sorry for being a tease). The new version seems kind of hacked together and since uploads have security issues, I figured I’d take some time to double check everything and try to speed things up as much as possible. I also want to tidy up the interface and get rid of the page reloading.

Well, that’s all for today, I should probably be getting to sleep now since I have to get up for work tomorrow.

Visual Basic 6.0 Example Archive

A discussion within the comments of one of my previous posts got me thinking about the past and how this site used to host a lot of neat programming examples. When I restarted the site last year one of my aims was to reinvent the site and to stay away from being an example depot. However, some of the examples I used to host weren’t available elsewhere and people did put a lot of effort into what they sent me. Not only that, the examples did seem to help people out.

When I made a post complaining about how all of my files were deleted, someone sent me a zip of all the programming examples they had downloaded from my site. The person had apparently made the zip one summer when they had decided to teach themselves how to program. The person told me they weren’t going to have net access during that summer so they had gone through my site and grabbed up all of the examples they thought would be useful.

Apparently they learned a decent amount and had archived some of them on their computer for future reference. I thought this was a pretty cool story, though since I was still in my “no old stuff” mind set, I didn’t post up the examples. However, thinking back about the VB Contest and Craig Jr, I decided it was worth putting up an archive of the old examples. I’ve re-created the old example page with everything listed, though some of the files are still missing. What’s available for download either comes from the zip I was sent or from archive.org. You can view the new example archive here:

Visual Basic 6.0 Example Archive

If anyone has any of the examples that are currently labeled “Missing” please let me know.

Forums

I’ve made my decision. There will be forums on this site, and soon. For now, give me around a week or two to get things set up – I want to iron out the details and play around with the forum software I have.

I know only a hand full of you said you’d be up for it, but there’s definitely a decent amount of passer byers / lurkers coming to this site, if interesting discussion cropped up I’m sure people would join in.

Also, thanks to everyone who replied in that thread.

Small Updates

  • The About page was updated.
  • The Ad format was changed for the blog. I figured the footer of each blog post would be a good place to stick them. I read up a little on inserting them inside of the actual blog post content, but that almost always annoys me when I see it so I went with the footer option.

Things Coming Soon

I’m working on a number of things but due to time issues everything seems to be taking forever. However, I hope to have TAAG and Image Color Palette Generator updates out soon.

Slider Puzzle Source Code and Other Nonsense

I’ve finally gotten around to releasing my Slider Puzzle source code. Hopefully someone out there can find a good use for it. I was tempted to set up a Jessica Alba puzzle on some free web hosting site and then link to it from here (as an example of how this code could be used), but I thought better of it.

At work, we do these things called “code reviews”, where everyone on the team reads over your code and gives you comments. It’s actually more helpful than one would think. Sometimes you don’t see small mistakes, like forgetting to update a comment, and sometimes people have suggestions on how to more gracefully do something. You actually learn a decent amount after attending a few of them. Occasionally someone will get ripped a new one, but that doesn’t happen too often.

I tried to give myself a code review before I released this code, but undoubtedly there will still be some imperfections somewhere. Hopefully the code is understandable though. If you decide to use it, let me know and I’ll give you a shout out on this blog.

Google Reader / Welcome to 2005

Despite all the press they’ve recieved, I’ve never used a feed reader. I knew what they were for, but for some reason I didn’t think I really needed one. When I re-started this site last year, someone emailed me asking me to put a link to the RSS feed, so I did, but I didn’t really bother trying out a feed reader myself. I finally got around to checking out Google Reader today, and honestly, I can’t believe I waited so long! This thing is great! You wouldn’t think they it would be that handy, but after setting things up, it’s actually a really nice tool.

Feed readers basically keep track of various sites you’re interested in and alert you when a new blog post / update occurs. In Google Reader, if you think a particular blog post / news story / whatever is interesting, you can share it with your “friends” (this is really your Gmail contacts, so you have to be careful – Google got some flap over this recently).

After playing around with it some, I went around to all the places I normally visit to see if they had links for feeds. Most didn’t, which was slightly annoying, but since I didn’t really bother paying attention to feed readers until recently, I can’t really blame other site owners. Anyway, if you’ve never bothered to check out a feed reader, I highly recommend taking a look at Google Reader (side notes: it’s free and I’m not being paid anything to talk about it or link it here).

Ads

After doing some experimentation, I’ve decided to keep the Google Ads strictly to the blog. They’re not generating a whole lot of clicks, but they’ve gotten a few. I placed them on a few choice pages for a while, but I decided to take them down because I think ads can cheapen the look certain pages (plus I felt kind of dirty having them on things like the VB Array tutorial). I think my rule of thumb will be to keep this site ad free unless I’m dealing with a) the blog or b) a page that’s getting 10,000+ views a day. A friend of mine gave me the second option as a rule of thumb for when to use ads and it seems to make sense (the ads can help deal with the bandwidth plus make you a couple of extra bucks). I don’t mind having them on the blog since I don’t think they come off as annoying, they can be blocked without the page looking strange, and since I don’t update every day, they can serve as somewhere to go if you’re truly bored (for those of you who click ads, whoever you are).

Ambitions / New Company

So yeah, I haven’t been doing all this ad experimentation simply for just this site. For about a year now I’ve been talking to some of my old grad school buddies about possibly starting a web 2.0 style site that could generate revenue. Nothing really came out of it at first since they were still finishing their thesis projects, however, now that one is done and the other is almost done, we’re actually making some progress. We haven’t coded anything yet, but we’ve spent the past month brainstorming ideas and narrowing things down. It actually looks like we’re going to make a run at this, which I think would be pretty cool. If you never hear me mention this again, you’ll know things fell through, but if the wheels keep turning I’ll make sure and keep you all posted on this.

New App This Friday / Programming Challenge

New App Coming

On Sunday I sat down and programmed up a new online app. I might tweak it a bit between now and then, but as of right now it’s essentially done. I’ll post it up later this week – most likely Friday or Saturday. Since I wrote it in one day, you can assume it’s pretty small, it’ll be cool though! I actually really like it (and I don’t like everything I make). It was on my list of “stuff to do on a rainy day”. Sunday wasn’t actually rainy, but I was tired and I realized I hadn’t released anything here in a while, so I thought I’d take a stab at it.

I had been working on another online app, which I’ve mentioned a few times here before, but it was a bit too ambitious and I’ve lost interest. So it’s offically been abandoned for the moment. I may come back to it later though.

Programming Challenge

While reading some more of Steve Yegge’s blog entries, I ran across a very interesting programming question. It came from an entry Yegge wrote on doing phone interviews. He would always ask potential candidates the following question (or a variation of it):

Last year my team had to remove all the phone numbers from 50,000 Amazon web page templates, since many of the numbers were no longer in service, and we also wanted to route all customer contacts through a single page.

Let’s say you’re on my team, and we have to identify the pages having probable U.S. phone numbers in them. To simplify the problem slightly, assume we have 50,000 HTML files in a Unix directory tree, under a directory called “/website”. We have 2 days to get a list of file paths to the editorial staff. You need to give me a list of the .html files in this directory tree that appear to contain phone numbers in the following two formats: (xxx) xxx-xxxx and xxx-xxx-xxxx.

How would you solve this problem? Keep in mind our team is on a short (2-day) timeline.

Aparently 25%-35% of the software developers he asked this question to could not solve it. So give yourself a pat on the back if you think you’ve got a solution. Once you’re sure on what you’d do, click here to see what the best solution would be. I was actually estatic that I got it, though I’ve been using Unix a lot at work, and I’ve been reading up on regexes, so it’d be pretty bad if I didn’t get it. If you’re not a programmer, reading that solution will make no sense to you. This is the blog entry it’s from. It’s basically an article on what you should know when you go into an interview for a software development position.

New Old Stuff

I decided to resurrect the “Most Asked For VB Code” section of my old site. I figured there were a lot of helpful bits of code in there, and that it was a waste to simply discard it. You can view it here:

VB 6.0 Code Bank

I went through and checked all of the code to ensure that it still works, and I tidied up some of the messier looking segments. It’s cool to look back at some of the code I wrote when I was 16/17 and think “hey, that’s pretty clever!” But then some of it also made me go “holy crap, I was an idiot!” I was especially embarrassed by how many times I used variables named “pat” or “patorjk”. I believe early on in my programming days I saw some hacker posting code on a message board and he would use his handle as a variable name. I thought that was pretty cool so I picked it up. Sadly, no one ever pointed out to me that this was a bad programming practice and that it made code hard to decipher.

I suppose that’s one of the disadvantages to teaching yourself. You can end up unaware of certain bad practices until they come back to haunt you. When I was a Teaching Assistant back in grad school I worked with a beginner class where students could lose up to 60% of their project points on style alone. I remember having irate students come to me and ask how they could have received a “D”, or in some cases an “F”, when their project worked perfectly.

I was kind of upset with the harsh grading guidelines too, but there wasn’t anything I could do. Looking back, I still think it’s too harsh to put so much of a weight on style. All that will do is discourage smart people who have picked up some bad habits. This reminds me of an essay I read recently:

Hackers and Fighters

I like how the author romanticizes the idea of the “street programmer” (the self-taught programmer), and I like how he points out the weak points in a university education. Though a university does do a good job of weeding out complete idiots, I’ve seen a number of people who can barely program (ex: “I can’t program in Java, I only know C++”) who go on to make 80k+ a year. Then there are people I know who are self-taught who could program circles around those people and they make less than 50k a year. It boggles my mind. A degree and good social/networking skills seem to be the most important things when getting a good job, which saddens me a little, but oh well.

Opera and Other Things

I downloaded the Opera web browser recently, just to see what this page looked like. I noticed that the TAAG program looked like crap so I’ve redone some of the CSS for it. Later this week I hope to do another update to it to add in some new features.

A while back I was working on a new web app, I haven’t touched it in over two weeks – my plan is to start working on it again later this week and to try and release it asap.

Some Minor Updates

VB Arrays Tutorial

I noticed from my stats page that chicanerous’ VB Arrays tutorial was getting between 8 and 10 views a day. This made me realize a decent number of people were reading it and I felt sort of bad that I had it displayed with such a crappy layout. The grey background and white text on a stand alone page made the tutorial look very low quality, so I decided to give it a make over and add a section on the Split and Join functions:

http://www.patorjk.com/programming/tutorials/vbarrays.htm

Also, there will be more updates to the programming section soon. It’s been pretty sparse for a while.

Links

I’ve created a formal link section. On my old website, I had a link exchange program. I will no longer do this. Mainly because I had a lot of crappy links submitted to me, and at the time I figured it was better to be nice and do the link exchange than to reject someone who probably visited my page regularly. However, I actually ended up having a few people email me asking why I had a couple of really crappy sites linked (I wont name names). I’m sure this caused them (and others) not to trust the link suggestions I gave. There was also the problem with people unlinking me after I linked them, which was annoying (especially if I didn’t like their site to begin with).

So now I’m just going to limit it to sites that I like, think are interesting, have useful/relevant content, and feel are worth checking out. As time goes on I hope to add a lot more than what’s there now. I haven’t decided if I want to focus on smaller sites or just interesting sites in general. My gut is to go with smaller sites, but we shall see.

Sleep

I haven’t been getting much sleep lately. I guess there’s no point to me saying that here, but I’m pretty tired right now and felt like sharing :P. There was actually going to be more added today, I just didn’t get around to it, maybe this weekend though.

Alternative Photomosaic Algorithms

One thing that has always bothered me is software patents. They just seem wrong. How can someone own a way of doing something? Or own a technique that others would come up with when trying to solve the same problem? They seem like unnatural restraints, like patenting the solution to a math problem. I can understand wanting to protect your ideas, but I honestly don’t think most software ideas are novel enough to warrant a patent, especially at the rate that the US government seems to be giving them out. I’m mean hell, who can be expected to know about all the stupid things people have patented? There is no fucking way that there were 40,000 patent-worthy software ideas that came out last year. That’s completely absurd.

One example that always comes to mind when of thinking about why software patents are bad is the story of what happened to id Software while they were developing Doom 3. Essentially John Carmack, the technical director at id Software, came up with a neat way of doing real time shadowing, which he called Carmack’s Reverse. After discussing the technique on his blog, it was discovered that two researchers had already patented the idea. In order to be able to use the technique, Carmack had to come to an agreement with them. Someone else, independently of these two parties, had also discovered the technique and presented it at a conference before the patent was filed. This person offered to let id Software use it for free, but id Software decided to play it safe and strike a deal with the patent owners [1].

Its things like that that bother me. Someone is toiling away writing a piece of code, they come up with a great way for solving a problem, implement it, and then later learn some researcher in a lab somewhere has patented the idea – probably just so they can say they have X number of patents – and now they have to pay to use a technique that they came up with on their own.

But I digress. I could go on for 10 pages about why I don’t like software patents. My focus here is that I’ve learned that a patent has been granted for the creation of photomosaics, those neat images that are made up of smaller images. The abstract reads as follows [2]:

A mosaic image is formed from a database of source images. More particularly, the source images are analyzed, selected and organized to produce the mosaic image. A target image is divided into tile regions, each of which is compared with individual source image portions to determine the best available matching source image by computing red, green and blue channel root-mean square error. The mosaic image is formed by positioning the respective best-matching source images at the respective tile regions.

I honestly don’t blame the person who filed this patent, since they were one of the first people to create mosaics with photos [3], and they wanted to make sure people didn’t profit at their expense. However, I still really dislike the idea of patenting a way of doing something, especially when the method seems rather obvious. I was able to write a photomosaic generation program when I was in 11th grade, and I didn’t have to look up an algorithm, I just made one up (yes, Mosaicer was written when I was in high school).

The only part of that abstract that seems outside the normal algorithm for creating your basic mosaic is the part that mentions the “root-mean square error” in relation to the color difference. That part is significant because it does a much better matching job than taking a simple color difference (what Mosaicer does). However, spend any time in a reading up on color differences, and you’ll learn that that’s the way to do color differences in RGB color space. So I’m not sure why this abstract as a whole was considered so novel.

Furthermore, the RGB color space is flawed in that it’s a non-uniform color space. Instead of using the RGB color space, one could use the L*a*b* color space, which is a uniform color space that has some of the most precise color difference formulas ever developed.

There’s also another way of creating photomosaics, outside of simply changing the color space and color difference methods. 90% of the people who read what follows will have no clue what I’m talking about, but try to follow along because this is a completely different way of patch matching than what is described in the patent abstract. Instead of dividing the target image into blocks and seeing which input images best fit the blocks, one can use Fast Fourier Transform patch matching [4, 5]. This technique wasn’t discovered until 2002, and I haven’t seen anyone discuss it with relation to creating photomosaics.

When using this method you transform you data into frequency space, perform some calculations, and then you transform your data back into the normal space. Because frequency space is a weird place, multiplication becomes addition. Therefore your computation time is greatly reduced when calculating squared color differences in frequency space. Kwatra et at [6] wrote about the speed up this matching technique provided in their paper on graph cut texture synthesis. With the simple RGB method, generation time for a particular video was 10 minutes, with the FFT method, generation time on the same video was 5 seconds. This is actually how I came across the FFT patch matching. I had to implement Kwatra’s paper for a graphics project I was assigned.

After reading the FFT paper, I immediately saw the possible application to photomosaics – however, after implementing the FFT patch matching method and seeing its nuts and bolts I wasn’t sure how much of a speed up it would actually provide, since it performs more calculations than needed for photomosaic patch matching (it tells you how well the image matched at every offset instead of just the subset of offsets needed for tile placement). In fact, I wondered if it could end up slowing things down. But there are ways of optimizing it for photomosaics, so it was (and is) unclear to me how much of a speed up or slow down effect it would have.

As you can probably tell, I never got around to trying out the above method in relation to photomosaics, mostly because I’m not that interested in photomosaics anymore. But I did put it on my list of possible things to do in the future. I will never patent any of these ideas discussed here so they’re free for anyone to use. However, I have no clue if someone has already thought of them and patented them. So I can’t make that guarantee. Though I figured I’d make a post noting all of this in case no patent for this exist, just so there are patent free methods for photomosaic creation out there.

References:
[1] http://techreport.com/onearticle.x/7113
[2] http://v3.espacenet.com/textdoc?DB=EPODOC&IDX=US6137498
[3] http://en.wikipedia.org/wiki/Photomosaic#History
[4] http://www.cs.sfu.ca/~torsten/Publications/Papers/icip02.pdf
[5] http://www.cs.sfu.ca/~mark/ftp/Icip02/images/icip_2002.jpg
[6] http://www.cc.gatech.edu/~turk/my_papers/graph_cuts.pdf