#!/usr/bin/perl -w # # raw2ql # ====== # # version 3 public release # copyright P KENT FEB 2001 selsyn@zoom.co.uk http://pages.zoom.co.uk/selsyn/ # # Changes from v2: # Autosenses the type of file be converted - Mode 4 or 8, colour or grey. # Uses appropriate dithering system. # now performs internal diffusion/stochastic dithering # input file must be called ql.raw / ql4.raw / ql8.raw # will run under strict # supressed printing of the $line, for speed # # DISCLAIMER: # This was written as a service to the QL community. # No responsibility will be assumed for anything this program does # Use at your own risk. # # OPERATION: # converts to QL video dump from raw format # Note: QL video is encoded as 2-byte chunks # NOTE! Save greyscale images as greyscale, not colour, for best results # # MODE 4 Format # msb->lsb (Green Red) # mode 4: GGGGGGGG RRRRRRRR # R+G=White # # MODE 8 Format # msb->lsb (Green Flash Red Blue) # mode 8: GFGFGFGF RBRBRBRB # use strict ; # we run under strict for testing my $ver = '3.02' ; # the version my $MODEFLAG = 0 ; # 0, 4, or 8 my $GREYFLAG = 0 ; # 0 (colour) 1 (grey) my $INPUT = 0 ; # has input file been found my $INFN = '' ; # input filename # you may wish to set these initial values for MODEFLAG, GREYFLAG etc. # and comment out the following file type detection my $line=0 ; # the current line of the bitmap my $px = 0 ; # the current chunk of the line my $thresh=254.99 ; # the byte-value that marks the 'white-level' my @SIZE = (393216,196608,131072,65536) ; # mode 4, 8, 4 grey, 8 grey : expected filesizes print "\n===== RAW2QL Version $ver (c) Piers Kent 2000 - 2001 =====\n" ; print "===== Email: selsyn\@zoom.co.uk =====\n" ; print "===== WWW: http://pages.zoom.co.uk/selsyn/ =====\n" ; if ( -e 'ql4.raw' && (-s 'ql4.raw' == $SIZE[0]) ) { $MODEFLAG = 4 ; $INPUT = 1 ; $INFN = 'ql4.raw' ; } if ( -e 'ql8.raw' && (-s 'ql8.raw' == $SIZE[0]) ) { $MODEFLAG = 8 ; $INPUT = 1 ; $INFN = 'ql8.raw' ; } if ( -e 'ql4g.raw' && (-s 'ql4g.raw' == $SIZE[0]) ) { $MODEFLAG = 4 ; $GREYFLAG = 1 ; $INPUT = 1 ; $INFN = 'ql4g.raw' ; } if ( -e 'ql8g.raw' && (-s 'ql8g.raw' == $SIZE[0]) ) { $MODEFLAG = 8 ; $GREYFLAG = 1 ; $INPUT = 1 ; $INFN = 'ql8g.raw' ; } if ( -e 'ql.raw' ) { if ( -s 'ql.raw' == $SIZE[0] ) { $MODEFLAG = 4 ; $INPUT = 1 ; $INFN = 'ql.raw' ; } if ( -s 'ql.raw' == $SIZE[1] ) { $MODEFLAG = 8 ; $INPUT = 1 ; $INFN = 'ql.raw' ; } if ( -s 'ql.raw' == $SIZE[2] ) { $MODEFLAG = 4 ; $GREYFLAG = 1 ; $INPUT = 1 ; $INFN = 'ql.raw' ; } if ( -s 'ql.raw' == $SIZE[3] ) { $MODEFLAG = 8 ; $GREYFLAG = 1 ; $INPUT = 1 ; $INFN = 'ql.raw' ; } } # you may wish to comment this out if your software saves RAW files with extra footer data # and hence makes the input files the 'wrong' size. if ( $INPUT == 0 ) { print "* Error : No input file or input file incorrect size.\n" ; exit 0; } # We now know which file we are using, which mode it is and whether it's greyscale or colour print "Opening files..." ; open (RAW,$INFN) or do {print("\n* Error : Cant open input file '$INFN'\n") ; exit 0 ; } ; open (CNV,"> img_ql$MODEFLAG") or do {print("\n* Error : Cant open output file 'img_ql$MODEFLAG'\n") ; exit 0 ; } ; print " done\n" ; # run appropriate conversion routine if ( $MODEFLAG == 4 && $GREYFLAG == 0 ) { &MODE4COLOUR() ; } if ( $MODEFLAG == 8 && $GREYFLAG == 0 ) { &MODE8COLOUR() ; } if ( $MODEFLAG == 4 && $GREYFLAG == 1 ) { &MODE4GREY() ; } if ( $MODEFLAG == 8 && $GREYFLAG == 1 ) { &MODE8GREY() ; } &close() ; print "Done!\n===============\n\n" ; exit(0) ; ################################################################ ### ### SUBROUTINES sub MODE8COLOUR() { print("Converting Mode 8 24-bit Colour Image...\n"); my ($ra,$rb,$rc,$rd,$ga,$gb,$gc,$gd,$ba,$bb,$bc,$bd,$byteone,$bytetwo,$n,@twpix); while ( $line < 256 ) { #print("$line\n"); # uncomment if you want a running display of where the program is $px=0; while ( $px < 64 ) { read RAW,$n,12; @twpix = split(//,$n); # Red Pixels $ra = ((ord $twpix[0]) > rand($thresh) ) * 128; $rb = ((ord $twpix[3]) > rand($thresh) ) * 32; $rc = ((ord $twpix[6]) > rand($thresh) ) * 8; $rd = ((ord $twpix[9]) > rand($thresh) ) * 2; # Green Pixels $ga = ((ord $twpix[1]) > rand($thresh) ) * 128; $gb = ((ord $twpix[4]) > rand($thresh) ) * 32; $gc = ((ord $twpix[7]) > rand($thresh) ) * 8; $gd = ((ord $twpix[10]) > rand($thresh) ) * 2; # Blue Pixels $ba = ((ord $twpix[2]) > rand($thresh) ) * 64; $bb = ((ord $twpix[5]) > rand($thresh) ) * 16; $bc = ((ord $twpix[8]) > rand($thresh) ) * 4; $bd = ((ord $twpix[11]) > rand($thresh) ) * 1; # Into QL data format $byteone = chr($ga + $gb + $gc + $gd); $bytetwo = chr($ra + $rb + $rc + $rd + $ba + $bb + $bc + $bd); print(CNV "$byteone$bytetwo"); $px++; } $line++; } } sub MODE8GREY() { print("Converting Mode 8 8-bit Greyscale Image...\n"); my ($ma,$mb,$mc,$md,$byteone,$bytetwo,$n,$v,@twpix); while ( $line < 256 ) { #print("$line\n"); # uncomment if you want a running display of where the program is $px=0; while ( $px < 64 ) { read RAW,$n,4; @twpix = split(//,$n); # Monochrome Pixels $ma = ((ord $twpix[0]) > rand($thresh) ) * 128; $mb = ((ord $twpix[1]) > rand($thresh) ) * 32; $mc = ((ord $twpix[2]) > rand($thresh) ) * 8; $md = ((ord $twpix[3]) > rand($thresh) ) * 2; # Into QL data format $v = $ma + $mb + $mc + $md ; $byteone = chr( $v ); $bytetwo = chr( ($v * 3) / 2 ); print(CNV "$byteone$bytetwo"); $px++; } $line++; } } sub MODE4GREY() { print("Converting Mode 4 8-bit Greyscale Image...\n"); my ($ma,$mb,$mc,$md,$me,$mf,$mg,$mh,$byteone,$n,@twpix); while ( $line < 256 ) { #print("$line\n"); # uncomment if you want a running display of where the program is $px=0; while ( $px < 64 ) { read RAW,$n,8; @twpix = split(//,$n); # Monochrome Pixels $ma = ((ord $twpix[0]) > rand($thresh) ) * 128; $mb = ((ord $twpix[1]) > rand($thresh) ) * 64; $mc = ((ord $twpix[2]) > rand($thresh) ) * 32; $md = ((ord $twpix[3]) > rand($thresh) ) * 16; $me = ((ord $twpix[4]) > rand($thresh) ) * 8; $mf = ((ord $twpix[5]) > rand($thresh) ) * 4; $mg = ((ord $twpix[6]) > rand($thresh) ) * 2; $mh = ((ord $twpix[7]) > rand($thresh) ) * 1; # Into QL data format $byteone = chr( $ma + $mb + $mc + $md + $me + $mf + $mg + $mh ); print(CNV "$byteone$byteone"); $px++; } $line++; } } sub MODE4COLOUR() { print("Converting Mode 4 24-bit Colour Image...\n"); my ($ra,$rb,$rc,$rd,$re,$rf,$rg,$rh,$ga,$gb,$gc,$gd,$ge,$gf,$gg,$gh,$byteone,$bytetwo,$n,@twpix); while ( $line < 256 ) { #print("\t$line\n"); # uncomment if you want a running display of where the program is $px=0; while ( $px < 64 ) { read RAW,$n,24; @twpix = split(//,$n); # Red Pixels $ra = ((ord $twpix[0]) > rand($thresh) ) * 128; $rb = ((ord $twpix[3]) > rand($thresh) ) * 64; $rc = ((ord $twpix[6]) > rand($thresh) ) * 32; $rd = ((ord $twpix[9]) > rand($thresh) ) * 16; $re = ((ord $twpix[12]) > rand($thresh) ) * 8; $rf = ((ord $twpix[15]) > rand($thresh) ) * 4; $rg = ((ord $twpix[18]) > rand($thresh) ) * 2; $rh = ((ord $twpix[21]) > rand($thresh) ) * 1; # Green Pixels $ga = ((ord $twpix[1]) > rand($thresh) ) * 128; $gb = ((ord $twpix[4]) > rand($thresh) ) * 64; $gc = ((ord $twpix[7]) > rand($thresh) ) * 32; $gd = ((ord $twpix[10]) > rand($thresh) ) * 16; $ge = ((ord $twpix[13]) > rand($thresh) ) * 8; $gf = ((ord $twpix[16]) > rand($thresh) ) * 4; $gg = ((ord $twpix[19]) > rand($thresh) ) * 2; $gh = ((ord $twpix[22]) > rand($thresh) ) * 1; # Into QL data format $byteone = chr($ga + $gb + $gc + $gd + $ge + $gf + $gg + $gh); $bytetwo = chr($ra + $rb + $rc + $rd + $re + $rf + $rg + $rh); print(CNV "$byteone$bytetwo"); $px++; } $line++; } } sub close() { print("Closing files...\n"); close(RAW); close(CNV); }