Monday, July 25, 2005

C String Manipulation

Like most people I too feel that perl string manipulation is much better than C. But recently I found this function called strsep(). It is the best possible function.
I am copying it below. Anyone and every1 who has ever parsed a file in C and found it difficult must read this.

char* strsep(char **stringp ,char *delim);
stringp is the initial string and delim is the delimiter character. The result is given as char* initial portion before the delimiter and rest is stored in stringp.

Eg. strcpy(string1,"shubham");
string2 = strsep(&string1,"u");

//Now string2 contains "sh" and string1 contains "bham"

Thursday, July 21, 2005

Wallpaper from Yahoo API Wallpaper Generator

Yahoo API wallpaper generator

#! /usr/local/bin/perl -w
#############################################################################
# Yahoo Image Search Wallpaper Generator
#
# Written by Shubham Singal
# shubham@gmail.com
#
# ############################################################################

use strict;
use warnings 'all';
use Getopt::Long;
use LWP::Simple;
use constant AppUrl => "http://upster.blogspot.com/2005/03/" .
"yahoo-image-search-collage-generator.html";

use Yahoo::Search AppId => "wallpaper_maker";

my $rows = 10;
my $columns = 10;
my $total = $rows * $columns;
my $width = 80;
my $height = 60;
my $query = "";
my $help = 0;

GetOptions(
'query=s' => \$query,
'rows=i' => \$rows,
'columns=i' => \$columns,
'width=i' => \$width,
'height=i' => \$height,
'help' => \$help,
);

if ($help) {
print qq/$0
--query The text to search images for
[--rows ] The number of rows in the output HTML table
[--columns ] The number of columns in the output HTML table
[--width ] The width of each image in the output HTML table
[--height ] The height of each image in the output HTML table
[--help] Displays this message/;
exit;
}

die "Must specify a query!\n" unless ($query);

my $folder = "folder_to_store_temp_images";
if(-d $folder)
{
system("rm -rf $folder");
}

system("mkdir $folder");
my $i=0;
print "Querying Yahoo...";

while($total>0)
{
my @results = Yahoo::Search->Results(
Image => $query,
Count => 50,
Start => $i
);
print "\n Query DONE!\n";

my $size = $width."x".$height;
foreach (@results)
{
my $url = $_->ThumbUrl() ;
if (getstore($url, "$folder/$i.jpg") eq 200)
{
system("convert $folder/$i.jpg -resize $size $folder/resize_$i.jpg ");
print " $i";
$i++;

}
}
$total = $total - 50;
}

$i--;
print "\n Creating Wallpaper\n";
my $rc = $rows."x".$columns;
system("montage $folder/resize_*.jpg -tile $rc -geometry +2+2 Wallpaper_$query.jpg");

if(-d $folder)
{
system("rm -rf $folder");
}

print "\n Wallpaper Created\n";

Tuesday, July 19, 2005

ImageMagick Tutorial

http://www.cit.gu.edu.au/~anthony/graphics/imagick6/