Wednesday, March 21, 2012

A new start

A while back my hosting service provider had a server crash. Unfortunately they could not recover the mysql data completely. That resulted in me losing my wordpress based blog.
I had a few posts on my local disk and today I have some free time, so I am restarting my blog with the few old posts I could retrieve from my disk..

Regards
kk

PCX image decoder / convertor in JAVA

This is a simple decoder / converter to jpg / png in Java for PCX image format.
It supports the Monochrome & 24 bit color images in PCX format. I will add support
for other formats as & when I come across them.

===================================================


/**

* @author Koustubha Kale

* koustubha_kale@yahoo.com

* I have made liberal use of the code I have found on the net in this,

* although I did not find one that actually worked. Whereas this does. You can
use this

* in the same manner if you find it usefull.

*/

import com.sun.jimi.core.*;

import com.sun.jimi.core.raster.*;

import com.sun.jimi.core.JimiException;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.color.ICC_ColorSpace;

import java.awt.color.ICC_ProfileRGB;

import java.awt.event.*;

import java.awt.image.*;

import java.beans.PropertyVetoException;

import java.io.*;

import java.util.*;

import javax.imageio.*;

import java.util.Arrays;



public class GetFormats {

public static void main(String args[]) {

//String readFormats[] = Jimi.getDecoderTypes();

//String writeFormats[] = Jimi.getEncoderTypes();

//System.out.println("Decoders: " + Arrays.asList(readFormats));

//System.out.println("Encoders: " + Arrays.asList(writeFormats));



try {

//Image im = Jimi.getImage("ims.pcx", Jimi.SYNCHRONOUS);

//System.out.println("Image Height="+im.getHeight()+", Image Width="+im.getWidth());


//System.out.println("Is err? "+im.isError());

//Jimi.putImage("image/jpg", im, "ims-1.jpg");

//System.out.println("Wrote Image.");

//loadImage("ASCPRIN.pcx");

loadImage("DR.pcx");

} catch (Exception e) {

System.out.println("Err :" + e);

e.printStackTrace();

}



}



public static void loadImage(java.lang.String imageFilename)

throws IOException, Exception {

// read the image head

byte[] head = new byte[128];

int bitPlane = 0, type = 0;

long fileLength = new File(imageFilename).length();

BufferedInputStream openFile =

new BufferedInputStream(new FileInputStream(imageFilename));

long readLen = openFile.read(head, 0, 128);



if ((head[3] & 0xff) == 1 && (head[65] & 0xff) == 1) {

type = 0;

bitPlane = 1;

} else if (head[3] == 1 && head[65] == 2) {

type = 1;

bitPlane = 2;

} else if (head[3] == 2 && head[65] == 1) {

type = 2;

bitPlane = 1;

} else if (head[3] == 1 && head[65] == 3) {

type = 3;

bitPlane = 3;

} else if (head[3] == 1 && head[65] == 4) {

type = 4;

bitPlane = 4;

} else if (head[3] == 4 && head[65] == 1) {

type = 5;

bitPlane = 1;

} else if (head[3] == 8 && head[65] == 1) {

type = 6;

bitPlane = 1;

} else if (head[3] == 8 && head[65] == 3) {

type = 7;

bitPlane = 3;

}

System.out.print("nPlanes=" + (head[65] & 0xff));

System.out.print("Image type =" + type);

int width =

((((int) head[9] & 0x0ff) << 8) | ((int) head[8] & 0xff))

- ((((int) head[5] & 0x0ff) << 8) | ((int) head[4] & 0xff))

+ 1;



int height =

((((int) head[11] & 0x0ff) << 8) | ((int) head[10] & 0xff))

- ((((int) head[7] & 0x0ff) << 8) | ((int) head[6] & 0xff))

+ 1;

System.out.print("; Width,height=" + width + "," + height);

int version = head[1];

System.out.println("; version=" + version);

int color = 0;

if (version == 2 || version > 3)

color = 1;

else

color = 0;



int bytesPerLine = //(int) head[67] & 0x0ff;

((((int) head[67] & 0x0ff) << 8) | ((int) head[66] & 0xff));

System.out.println("bytesPerLine=" + bytesPerLine);



if (type == 0) {

if ((bytesPerLine*8)>width)

width=bytesPerLine*8;

BufferedImage innerImage =

new BufferedImage(

width,

height,

BufferedImage.TYPE_BYTE_BINARY);

java.awt.image.WritableRaster raster = innerImage.getRaster();



int[] unComData = new int[width * height];

int inc = 0;

byte[] tempAry = new byte[1];



while ((readLen = openFile.read(tempAry)) != -1) {



if (((int) tempAry[0] & 0xff) < 192) {

//for (int j = 0; j < 8; j++)

unComData[inc] = (int) tempAry[0] & 0xff;

//inc += 8;

//System.out.println("Data value="+((int) tempAry[0] & 0xff));

inc++;

} else {

int count = ((int) tempAry[0]) & 0x3f;

readLen = openFile.read(tempAry);

//System.out.println("multiple Data value="+((int) tempAry[0] & 0xff));

//System.out.println("Count="+count);

for (int j = 0; j < count; j++)

unComData[inc + j] = (int) tempAry[0] & 0xff;

inc += count;

}



}





System.out.println(

"Palett?=" + (unComData[unComData.length - 770] & 0xff));

System.out.println("inc=" + inc);

System.out.println("unComData length=" +unComData.length);

int[] data2 = new int[width * height];

int k = 0;





for (int j = 0; j < inc; j++) {

int temp = unComData[j];



for (int i = 1; i < 255; i *= 2, k++) {



//System.out.println("i="+i);

if ((temp & 0x80) == 0x80)

data2[k] |= 0x01;

else

data2[k] = 0x00;

temp <<= 1;

}

}



System.out.println("data2 length=" + k);

try {

raster.setPixels(0, 0, width, height, data2);



innerImage =

new BufferedImage(

innerImage.getColorModel(),

raster,

false,

null);

/*JFrame frame=new JFrame();

JLabel l=new JLabel();

l.setIcon(new ImageIcon(innerImage));

frame.getContentPane().add(l);

frame.pack();

frame.show();

*/



Jimi.putImage("image/jpg", innerImage, "ASCPRIN.jpg");

Jimi.putImage("image/png", innerImage, "ASCPRIN.png");

ImageIO.write(innerImage, "jpg", new File("ASCPRIN11.jpg"));



} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("set image error!");

e.printStackTrace();

}

}

if (type == 7) {

BufferedImage innerImage =

new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

java.awt.image.WritableRaster raster = innerImage.getRaster();



int[] unComData = new int[width * height * 3];

int inc = 0;

byte[] tempAry = new byte[1];



while ((readLen = openFile.read(tempAry)) != -1) {



if (((int) tempAry[0] & 0xff) < 192) {

unComData[inc] = (int) tempAry[0] & 0xff;

inc++;

} else {

int count = ((int) tempAry[0]) & 0x3f;

readLen = openFile.read(tempAry);



//System.out.println("Count="+count);

for (int j = 0; j < count; j++)

unComData[inc + j] = (int) tempAry[0] & 0xff;

inc += count;

}



}



int[] rAry = new int[bytesPerLine];

int[] gAry = new int[bytesPerLine];

int[] bAry = new int[bytesPerLine];

int len = unComData.length;

for (int i = 0; i < len; i += bytesPerLine * 3) {

for (int j = 0; j < bytesPerLine; j++) {

rAry[j] = unComData[i + j];

// System.out.println(i+bytesPerLine+j);

gAry[j] = unComData[i + bytesPerLine + j];

bAry[j] = unComData[i + 2 * bytesPerLine + j];

}



for (int j = 0; j < bytesPerLine; j++) {



//for (int k = 0; k < 3; k++) {

unComData[i + (j * 3)] = rAry[j];

unComData[i + (j * 3) + 1] = gAry[j];

unComData[i + (j * 3) + 2] = bAry[j];



//}

}

}

System.out.println("unComData length=" + unComData.length);

System.out.println("inc=" + inc);



try {

raster.setPixels(0, 0, width, height, unComData);

//ColorModel cm=new DirectColorModel(java.awt.color.ColorSpace.getInstance(java.awt.color.ColorSpace.CS_sRGB),false,false,Transparency.TRANSLUCENT,DataBuffer.TYPE_BYTE);


innerImage =

new BufferedImage(

innerImage.getColorModel(),

raster,

false,

null);

Jimi.putImage("image/jpg", innerImage, "joshi-2.jpg");

Jimi.putImage("image/png", innerImage, "joshi-2.png");

ImageIO.write(innerImage, "jpg", new File("joshi.jpg"));

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("set image error!");

e.printStackTrace();

}





}



//

openFile.close();

}



}
//end of file..
=========================================================


Enjoy..
Regards
kk

Monitor the bandwidth of your D-Link ADSL router from a linux box

Many ISP's provide the D-Link DSL-502T ADSL router with their Tri-Band broadband connection.
This simple howto allows you to monitor the bandwidth of your D-Link ADSL router from a linux box.
The procedure should work for any other router allowing snmp too..
There are lots of cool things you can do with mrtg. visit www.mrtg.org

You can log in to the D-Link DSL-502T ADSL router via web browser. By default its set up at http://192.168.1.1 default username & password are 'admin'.
Click on advance tab at the top, enable the checkboxes of Enable SNMP Agent & Enable SNMP Traps. Ensure community name is public ( you can change it to whatever you like but then just match the same in the following cfgmaker command.), Click apply. Go to tools on top tab, system on left hand tab & click on 'Save & Reboot' to save your setting & restart the router. Thats it for the router.
Now for the linux configuration part.
The following is for a RedHat / Fedora type system. You can adapt easily for any Linux flavor.

$yum -y install httpd mrtg net-snmp-utils # In case you don't have these installed already.
$cfgmaker --noreversedns public@192.168.1.1 --ifdesc=name --ifref=name --output=~/dlinkmrtg.cfg
## match the community name 'public' and / or IP of your router.

Edit the file dlinkmrtg.cfg with your favorite text edit
## You may want to un-comment the growright & bits line.
#Options[_]: growright, bits
## Also you may want to run mrtg in daemon mode at a set frequency.
## If so add following two lines in the global config of dlinkmrtg.cfg ( Without the comment # ofcourse)
#RunAsDaemon:Yes
#Interval:5
## runs mrtg every 5 minutes

$cd /var/www/mrtg
$indexmaker ~/dlinkmrtg.cfg --output=index.html
$env LANG=C /usr/bin/mrtg ~/dlinkmrtg.cfg  #run this command at least 3 times..

Edit /etc/httpd/conf.d/mrtg.conf to allow whoever u want to access the mrtg graphs.
By default it only allows localhost.

$echo 'env LANG=C /usr/bin/mrtg ~/dlinkmrtg.cfg' >> /etc/rc.local
to start mrtg at boot or you can comment the RunAsDaemon:Yes in dlinkmrtg.cfg & run above through cron. Whatever..

Start httpd service on your linux box
$service httpd start

Visit http://<IP of your Linux Box>/mrtg/

Enjoy
Regards
kk

Changing Koha's default logo

Koha's system preference "opacsmallimage" has always
amused me because of its limitation of size. Most of the time it won't
fit your libraries logo.
Here I have a jquery based script that can be used in "opacuserjs"
which overcomes this limitation. While replacing Koha logo the script
also adds a "Powered by Koha" with Koha logo and a link back to the
koha-community.org site at the bottom of the page. The script takes
care to add it below any credits you might have added by using the
"opaccredits" syspref.
Here goes the script that goes in your opacuserjs syspref..

$(document).ready(function(){

var logo='http://www.anantcorp.com/images/resized/anant-logo.gif';

var bkgnd=$("#opac-main-search").css("background-color");

var frgnd=$("#moresearches").css("color");

$("#opac-main-search").find("h1").remove();

$("#opac-main-search").prepend("<a
id='#libraryname' href='/cgi-bin/koha/opac-main.pl' style='float: left;
padding: 0pt; display: inline; margin: 0em 1em 0.3em 0.1em;'><img
src="+logo+" /></a>");

$("#fluid-offset").css({ "float":"left","width":"75%" });

if($(".ft").length != 0) {

$(".ft").after("<div
id='kohacredit'><center><b
style='color:"+frgnd+";'>Powered By :</b><a
href='http://www.koha-community.org' style='text-decoration:none;'
target='_blank' > <img src='/opac-tmpl/prog/images/koha-logo.gif'
alt='Koha' ALIGN='ABSMIDDLE'
style='background:"+bkgnd+";'/>&nbsp;</a></center></div>");

}

else {

$("div#bd").after("<div
id='kohacredit'><center><b
style='color:"+frgnd+";'>Powered By :</b><a
href='http://www.koha-community.org' style='text-decoration:none;'
target='_blank' > <img src='/opac-tmpl/prog/images/koha-logo.gif'
alt='Koha' ALIGN='ABSMIDDLE'
style='background:"+bkgnd+";'/>&nbsp;</a></center></div>");

}

});

On line number two of the script you can give link to your logo.
On line three you can set the background color for the transparent Koha
logo image and on line four the color of the words "Powered by :" is
set. I have tried to read in the values of Koha OPAC's search block's
background and text color here but you can simply replace these with
your favorite colors.
Enjoy..
Regards
KK