From 1d19687f3e89d4b5de10830a6210dd6c7e72225c Mon Sep 17 00:00:00 2001 From: Ed J Date: Mon, 1 Jul 2024 23:38:35 +0100 Subject: [PATCH] extract out image-regularising func --- lib/PDL/Graphics/Gnuplot.pm | 41 +++++++++++++++++++------------------ t/plot.t | 4 ++++ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/PDL/Graphics/Gnuplot.pm b/lib/PDL/Graphics/Gnuplot.pm index 13b32e5..bf1434f 100644 --- a/lib/PDL/Graphics/Gnuplot.pm +++ b/lib/PDL/Graphics/Gnuplot.pm @@ -3534,26 +3534,10 @@ EOF if ( $cdims==2 ) { if ($chunk{options}{with}[0] eq 'image') { my $dp = $dataPiddles[-1]; - if ($dp->ndims==3) { - if ($dp->dim(1) >= 5) { - if ($dp->dim(0) ==3 && $dp->dim(1) >= 5 && $dp->dim(2) >= 5) { - $chunk{options}{with}[0] = 'rgbimage'; - pop @dataPiddles; - push @dataPiddles,$dp->using(0,1,2); - } elsif ($dp->dim(2)==3 && $dp->dim(1)>=5 && $dp->dim(0) >= 5) { - $chunk{options}{with}[0] = 'rgbimage'; - pop @dataPiddles; - push @dataPiddles,$dp->mv(2,0)->using(0,1,2); - } elsif ($dp->dim(0)==4 && $dp->dim(1) >= 5 && $dp->dim(2) >= 5) { - $chunk{options}{with}[0] = 'rgbalpha'; - pop @dataPiddles; - push @dataPiddles,$dp->using(0,1,2,3); - } elsif ($dp->dim(2)==4 && $dp->dim(0) >= 5 && $dp->dim(1) >= 5) { - $chunk{options}{with}[0] = 'rgbalpha'; - pop @dataPiddles; - push @dataPiddles, $dp->mv(2,0)->using(0,1,2,3); - } - } + if ($dp->ndims==3 and $dp->dim(1) >= 5) { + my ($with, @ndarrays) = _regularise_image($dp); + $chunk{options}{with}[0] = $with; + splice @dataPiddles, -1, 1, @ndarrays; } } } @@ -3677,6 +3661,23 @@ FOO return (\@chunks, $Ncurves); } +sub _regularise_image { + my ($dp) = @_; + my $with = $dp->dim(0) == 3 || $dp->dim(2) == 3 ? 'rgbimage' : + $dp->dim(0) == 4 || $dp->dim(2) == 4 ? 'rgbalpha' : undef; + my @ndarrays; + if ($dp->dim(0) == 3 && $dp->dim(2) >= 5) { + @ndarrays = $dp->using(0..2); + } elsif ($dp->dim(2) == 3 && $dp->dim(0) >= 5) { + @ndarrays = $dp->mv(2,0)->using(0..2); + } elsif ($dp->dim(0) == 4 && $dp->dim(2) >= 5) { + @ndarrays = $dp->using(0..3); + } elsif ($dp->dim(2) == 4 && $dp->dim(0) >= 5) { + @ndarrays = $dp->mv(2,0)->using(0..3); + } + ($with, @ndarrays); +} + ########## # matchDims: nested sub inside plot - kludge up thread style matching across # the data arguments to a given chunk. diff --git a/t/plot.t b/t/plot.t index c8d6a52..7a77c05 100644 --- a/t/plot.t +++ b/t/plot.t @@ -150,6 +150,10 @@ unlink($testoutput) or warn "\$!: $! for '$testoutput'"; my $r9 = rvals(9,9); eval {$w->plot({colorbox => 1},{with => 'image'},$r9->xvals,$r9->yvals,$r9)}; is($@, '', "colorbox succeeded"); + for my $dims ([3,9,9],[4,9,9],[9,9,3],[9,9,4]) { + eval {$w->plot({colorbox => 1},{with => 'image'},$r9->xvals,$r9->yvals,rvals(@$dims))}; + is($@, '', "regularising image succeeded (@$dims)"); + } } {