On Sun, Nov 23, 2008 at 08:29, loody <[EMAIL PROTECTED]> wrote:
> 2008/11/23 Chas. Owens <[EMAIL PROTECTED]>:
>>
>>
>> On Nov 23, 2008, at 2:52, loody <[EMAIL PROTECTED]> wrote:
>>
>>> Dear all:
>>> The prototype of read is
>>> read FILEHANDLE,SCALAR,LENGTH
>>> ex:
>>> read PATTERN, $line, 1920;
>>>
>>> that means the $line will content 1920 bytes.
>>> if I want to modify the byte offset 720 of $line, it seems impossible,
>>
>> But happily it isn't.  You have several choices, but without more
>> information I would suggest substr*:
>>
>> substr($line, 720, 1) = $new_byte;
>>
>> Or
>>
>> substr($line, 720, 1, $new_byte);
> Hi:
> thanks for your kind help.
> I apologize for not explain my problem detail.
> Below is my source code:
>            $end_gold=sysread GOLDEN, $tmp_gold, $image_resolution_w[$idx];
>            $end_result=sysread RESULT, $tmp_result, $image_resolution_w[$idx];
>            if($tmp_gold ne $tmp_result)
>            {
>                print CMPREPORT "Frame index: $frame, UV line:
> $tmp_height error\n";
>            }
> As you can see, I only can tell which line of UV part in both images
> get different.
> Right now I want to high light the error part of this 2 lines as 0xFF,
> such that I can see the error part as bright red.
> Your suggestion is good, but the difficulties comes I don't exactly
> know where the error offset of these 2 lines.
> I got a stupid idea as read 2 files byte to byte to 2 arrays and
> compare them one by one.
> But it is really un-efficient.
>
>
>>
>> Depending on which you prefer.  Also, if you are going to be working with
>> raw bytes it is a good idea to say
>>
>> use bytes;
>>
>> At the top of your program to avoid unicode issues.
>
> I have look the page you give to me, http://perldoc.perl.org/bytes.html
> it seems I may not need it in my case, since I compare 2 lines of 2
> images and rewrite the error part.
> If I am wrong, please let me know.
> appreciate your help,
> miloody
>

Try this.  The best way to run it is

./script | less -R

The -R on less preserves the meaning of the ANSI color escapes.

#!/usr/bin/perl

use strict;
use warnings;

#make string functions work with a byte at a time
#rather than one character at a time (some characters
#are more than one byte)
use bytes;

use constant SAME                  => "\x{03}0";  #default terminal color
use constant DIFF                  => "\x{03}5";  #bold red
use constant GOLDEN_BUT_NOT_RESULT => "\x{03}7";  #bold blue
use constant RESULT_BUT_NOT_GOLDEN => "\x{03}13"; #bold yellow
use constant WIDTH                 => 40;

#ensure we open all files as binary rather than
#some encoding or in crlf modes.
use open IN => ":bytes";

use Color::Output;

Color::Output->Init;

die "usage: $0 golden result\n" unless @ARGV == 2;

open my $golden_fh, "<", $ARGV[0]
        or die "could not open golden file $ARGV[0]: $!";

open my $result_fh, "<", $ARGV[1]
        or die "could not open result file $ARGV[1]: $!";

local $/ = \1; #read one byte at a time

my $width = 0;
while (1) {
        rest_are_diff($result_fh, 1) unless defined(my $golden = <$golden_fh>);
        rest_are_diff($golden_fh, 0) unless defined(my $result = <$result_fh>);

        my $color = $golden eq $result ? SAME : DIFF;

        #I am giving $result preference when they are different
        #this is abritary
        print_byte($color, $result);
}

{
        my $width = 0;
        sub print_byte {
                my ($color, $byte) = @_;
                cprintf "$color %02x\x{03}0", ord($byte);
                #printf "%02x ", ord($byte);

                if (++$width > WIDTH) {
                        $width = 0;
                        print "\n";
                }
        }
}

sub rest_are_diff {
        my ($fh, $which) = @_;
        my $color = $which ? GOLDEN_BUT_NOT_RESULT : RESULT_BUT_NOT_GOLDEN;
        print_byte($color, $_) while <$fh>;
        print "\n";
        exit;
}


Of course, I think I would prefer something more like this:
#!/usr/bin/perl

use strict;
use warnings;

#make string functions work with a byte at a time
#rather than one character at a time (some characters
#are more than one byte)
use bytes;

#ensure we open all files as binary rather than
#some encoding or in crlf modes.
use open IN => ":bytes";

die "usage: $0 golden result\n" unless @ARGV == 2;

open my $golden_fh, "<", $ARGV[0]
        or die "could not open golden file $ARGV[0]: $!";

open my $result_fh, "<", $ARGV[1]
        or die "could not open result file $ARGV[1]: $!";

local $/ = \1; #read one byte at a time

my $width = 0;
while (1) {
        rest_are_diff($result_fh, 1) unless defined(my $golden = <$golden_fh>);
        rest_are_diff($golden_fh, 0) unless defined(my $result = <$result_fh>);

        unless ($golden eq $result) {
                printf "at offset $. bytes differed golden 0x%02x result 
0x%02x\n",
ord $golden, ord $result;
        }
}

sub rest_are_diff {
        my ($fh, $which) = @_;
        printf "not in " . ($which ? "golden" : "result") . " at offset $.:
0x%02x\n", ord $_ while <$fh>;
        exit;
}


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to