Monday, August 08, 2005

Write a TIFF in Java

At time of writing the documentation for the Java Image I/O API is a bit sparse, particularily when it comes to file types other than JPG, PNG or GIF.
Although the API is actually incredible easy to use, it took me a couple of hours to find a straightforward way of writing a compressed TIFF that actually worked. I use "PackBits" because it was the only kind of compression that produced a usable TIFF in the application I was targetting (Freehand). Here is my code. Sorry about the lack of indentation. You can download the snippet. Use freely at your own risk.

protected boolean saveTiff(String filename, BufferedImage image) {

File tiffFile = new File(filename);
ImageOutputStream ios = null;
ImageWriter writer = null;

try {

// find an appropriate writer
Iterator it = ImageIO.getImageWritersByFormatName("TIF");
if (it.hasNext()) {
writer = (ImageWriter)it.next();
} else {
return false;
}

// setup writer
ios = ImageIO.createImageOutputStream(tiffFile);
writer.setOutput(ios);
TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.ENGLISH);
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// see writeParam.getCompressionTypes() for available compression type strings
writeParam.setCompressionType("PackBits");

// convert to an IIOImage
IIOImage iioImage = new IIOImage(image, null, null);

// write it!
writer.write(null, iioImage, writeParam);

} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;

}

Wednesday, August 03, 2005

Fireworks MX: HTML Preview without margin

This is a quickie. Maybe there's another way to do this but anyway ...
If you press F12 in Fireworks MX you get a preview of what you're working on in a browser - useful when designing a webpage.
By default, the page you see has a white margin around it which is a bit annoying.
To get around this, first change the HTML style (File->HTML Setup) from the default 'Dreamweaver' to 'Generic'. Then edit the file called 'SLICES.XTT', which I found here:
C:\Program Files\Macromedia\Fireworks MX 2004\Configuration\HTML Code\Generic.
In this file replace:

WRITE_HTML("<body bgcolor=\"#", exportDoc.backgroundColor.toString(16), "\"");

with

WRITE_HTML("<body marginwidth=\"0\" marginheight=\"0\" leftmargin=\"0\" topmargin=\"0\" bgcolor=\"#", exportDoc.backgroundColor.toString(16), "\"");


This got rid of the margins for me. Try at your own risk.

Tuesday, July 26, 2005

Page Numbering in Freehand MX (Windows)

Freehand isn't designed for creating very long multi-page documents. It is primarily a drawing rather than a layout programme. Nevertheless many people are so comfortable working with it that they do try to use it to create longer documents. As one of these fools myself, I was horrified to discover that Freehand does not as yet support page numbering. There is an Xtra for the Mac which looks pretty good, but PC users must make do with a pretty embarrassing and not very useful workaround .

I rashly decided to whip together an xtra myself, thinking this would take me only a couple of hours. Twenty-four hours later, here is the result: Page Number.xtra

There is a readme.txt in the .zip file. here is some of what it says:

SIMPLE INSTRUCTIONS

1) Put the Xtra in your freehand Xtras folder.
2) Restart Freehand if it is already launched.
3) Put EXACTLY the following text on each page where you want the page numbers to appear:
_PNUM_
- This bit of text <_pnum_>, will be replaced by the appropriate number when you run the Xtra. If you type "Page _PNUM_." somewhere, this should be converted to "Page 1." on the first page of your document etc. Look at the example file 'example.fh8' if you're not sure what to do.
4) Before printing, or exporting, run the xtra - its under 'Other' on the Xtras menu. - Maybe this is called something different in non-English versions.
5) Note that:
* the action performed by the Xtra should be undoable.
* the Xtra will unfortunately not work with Master pages - so you will have to release any child pages before running it.
* I haven't tested it that much (only in Freehand MX), so save your work first.

-

A couple of notes about developing Freehand Xtras.
1) If you don't want to read the whole PDF manual delivered with Macromedia's XDK , do read the stuff about commands, in particular, the following sentence:
"Any and all modifications to the current document must be constructed as one or more commands."
- My failure to read this cost me many a perplexed hour. If you don't embed your document-altering code within a command, NOTHING will happen.
2) This is an API with a sense of humour, Hidden away in the vast API doc I found this function :

FHIMtxCreateWorldPeace
void FHIMtxCreateWorldPeace(
PIMoaFhMtx This,
MoaBoolParam andHarmony)
This call is not yet implemented, but have you hugged your mother today?
This A pointer to the IMoaFhMtx object.
andHarmony Pass in TRUE if harmony should be applied to the resulting peace, FALSE if you
don't care for harmony.
Returns contentment.
:-)

Wednesday, July 20, 2005

Accessing LAN websites under OSX (Panther) served by Apache on Windows

Like most web developers, I test my sites on a web server running locally on my machine - at the moment that's Apache 2.048 under Windows XP.
I set up virtual servers for Apache so I can develop relative to a domain root.
For a couple of years I've lived with a problem when testing a site from another machine on the LAN - for example my MacMini (OSX - Panther): While I see a site under 'http://mysite" on my PC , on the MiniMac I have to enter something like "http://192.168.0.1/mysite" - this is not only annoyingly longer, If I'm working with absolute paths, I have to reconfigure the site in order to test it.
The solution is not too difficult, but I couldn't find a straightforward example, so here is my working setup . (Note. This is an example rather than an explanation. It isn't for newbies. If you don't know what httpd.conf is, then this example probably isn't for you):

1. The Apache Virtual Server configuration. Here is the relevant section of my httpd.conf. Note the use of '*' after NameVirtualHost and VirtualHost.



NameVirtualHost *
<VirtualHost *>
    ServerAdmin admin@localhost
    DocumentRoot "E:\WWW\path_to\mysite"
    ServerName mysite
    ServerAlias *.mysite
    ErrorLog logs/mysite.log
    <Directory "E:\WWW\path_to\mysite">
        Options All Includes Indexes
    </Directory>
</VirtualHost>




2. And here the hosts file on XP (something like C:\WINDOWS\system32\drivers\etc\hosts)

...
127.0.0.1 a_site
127.0.0.1 mysite
127.0.0.1 another_site
...
3. So now you've got get http://mysite working locally (Once you've restarted apache of course). Next you need to set up the Mac. I use NetInfo to edit the OS X equivalent of the hosts file. Here's what my setup looks like:


so you create an entry under machines. give it the ip_address of the server (in this case the PC running Apache), and the name of the site.

Now http://mysite works on the MiniMac too! This saved me a lot of hassle.

Sunday, July 17, 2005

Track Amazon Sales Rank in PHP

There are wonderful tools that will do this (and more) for you (e.g. junglescan), but at time of writing, only for sales at Amazon.com.
This rather crude PHP script can be adapted to also check sales rankings at other Amazon sites. The script records rankings for any day on which it is opened. If you had access to cron jobs you could run it automatically on a daily basis.