[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Converting a page of text into a bitmap



Dear MacPerl-gurus,

I hope this is the right one of the MacPerl forums...

I've written a script which I use to convert between a propriety 
graphics format (Biorad confocal microscope files) and multilayer 
Photoshop files.

The script works OK but is pretty slow, and one of the slowest bits 
is the assembly of an 'information layer' that I put in the 
background layer of the photoshop file.

Basically, what I need to do is convert a bunch of text into a 
bitmap. The subroutine below (somewhat simplified from what I 
actually use) does what I want by producing a window with the text in 
it and then capturing the window pixel-by-pixel with GetPixel. Is 
there a better/faster way of achieving the same result? Would using 
an offscreen graphics world make any significant difference?

Thanks for any help/advice you can give. Additional advice on 
improving my nascent perl skill would also be welcome.

David

- ---snippet---
(using Mac::Windows, Mac::TextEdit, Mac::QuickDraw and some others.

$null = pack "H2", "00";
$max  = pack "H2", "FF";

sub BackgroundInfo {

	#Passed a source graphics object (and the destination 
graphics object through method call).
	#This method returns a suitably sized bitmap representation 
of 'notes' stored in the source graphics file.


	my $photoshop = shift;
	my $source = shift;

	my $windx = $photoshop->{y}; #Get the target bitmap size. 
Photoshop seem to mix up x and y to my mind...
	my $windy = $photoshop->{x};
	my @text;

	#If there aren't any notes return a plain black rectangle
	unless (@text = $source->readnotes) {
	    return $photoshop->BlackChannel($null);
	}


	#Make a window to write the text into:
	my $winr = Rect->new(50,50,$windx+70, $windy+70);
	my $win = MacWindow->new($winr, "", 1, floatProc(), 1);

	my $framer = Rect->new(5,35,$windx+15, $windy+15);
	my $textr = Rect->new(10 ,40, $windx+10, $windy+10);
	my $textedit = $win->new_textedit($textr, $framer);

	#Write the text in the window. No checks to make sure it'll all fit...
	$textedit->{edit}->txSize(9);
	TextSize(9);
	my ($ascent, $descent,$maxwidth,$leading) = GetFontInfo();
	$textedit->{edit}->lineHeight($ascent+$descent+$leading-2);
	$textedit->{edit}->fontAscent($ascent-2);
	$textedit->{edit}->txFace(32);
	TESetText("@text", $textedit->{edit});

	#Make sure the window is displaying everything right
	$win->update;

	#Get the bitmap pixel-by-pixel, surely there is an easier way?
	my ($i, $j);
	my @result = "";
	my $maxy = $windy+10;
	my $maxx = $windx+10;


	for ($i=10; $i<$maxy; $i++) {
		for ($j=10; $j<($maxx); $j++) {
			push @result, (GetPixel($j,$i) ? $max : $null);

		}
	}

	$win->dispose;

	unshift @result, $null; #Bitmap must be preceded by two nulls 
to indicate non-run-length-encoded
	unshift @result, $null;

	return @result;

END {
      $win->dispose() if (defined($win));
}

}

- ---------------------------
-- 
David Micklem   Dept. of Anatomy, Cambridge University, UK

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org