@@ -487,86 +487,81 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
487
487
interpolation_stage = 'rgba'
488
488
else :
489
489
interpolation_stage = 'data'
490
-
491
- if A .ndim == 2 and interpolation_stage == 'data' :
492
- # if we are a 2D array, then we are running through the
493
- # norm + colormap transformation. However, in general the
494
- # input data is not going to match the size on the screen so we
495
- # have to resample to the correct number of pixels
496
-
497
- # First compute out_mask (what screen pixels include "bad" data
498
- # pixels) and out_alpha (to what extent screen pixels are
499
- # covered by data pixels: 0 outside the data extent, 1 inside
500
- # (even for bad data), and intermediate values at the edges).
501
- if self .cmap .n_variates == 1 :
502
- mask = (np .where (A .mask , np .float32 (np .nan ), np .float32 (1 ))
503
- if A .mask .shape == A .shape # nontrivial mask
504
- else np .ones_like (A , np .float32 ))
505
- else :
506
- _maskNd = [(np .where (a .mask , np .float32 (np .nan ), np .float32 (1 ))
507
- if A .mask .shape == a .shape # nontrivial mask
508
- else np .ones_like (a , np .float32 ))
509
- for a in cm ._iterable_variates_in_data (A )]
510
- mask = np .prod (_maskNd , axis = 0 )
511
-
512
- # we always have to interpolate the mask to account for
513
- # non-affine transformations
514
- out_alpha = _resample (self , mask , out_shape , t , resample = True )
515
- del mask # Make sure we don't use mask anymore!
516
- out_mask = np .isnan (out_alpha )
517
- out_alpha [out_mask ] = 1
518
- # Apply the pixel-by-pixel alpha values if present
519
- alpha = self .get_alpha ()
520
- if alpha is not None and np .ndim (alpha ) > 0 :
521
- out_alpha *= _resample (self , alpha , out_shape , t , resample = True )
522
- # Resample data
523
- if self .cmap .n_variates == 1 :
524
- output = self ._resample_and_norm (A , self .norm , out_shape ,
525
- out_mask , t )
526
- else :
527
- output = [self ._resample_and_norm (a , self .norm [i ],
528
- out_shape , out_mask , t )
529
- for i , a in enumerate (cm ._iterable_variates_in_data (A ))]
530
- else :
531
- if A .ndim == 2 : # _interpolation_stage == 'rgba'
490
+ scalar_alpha = self ._get_scalar_alpha ()
491
+ if A .ndim == 2 :
492
+ if interpolation_stage == 'rgba' :
493
+ # run norm -> colormap transformation and then rescale
494
+ self .nac .autoscale_None (A )
495
+ unscaled_rgba = self .nac .to_rgba (A )
496
+ output = _resample ( # resample rgba channels
497
+ self , unscaled_rgba , out_shape , t , alpha = scalar_alpha )
498
+ # transforming to bytes *after* resampling gives improved results
499
+ if output .dtype .kind == 'f' :
500
+ output = (output * 255 ).astype (np .uint8 )
501
+ else : # if _interpolation_stage != 'rgba':
502
+ # In general the input data is not going to match the size on the
503
+ # screen so we have to resample to the correct number of pixels
504
+
505
+ # First compute out_mask (what screen pixels include "bad" data
506
+ # pixels) and out_alpha (to what extent screen pixels are
507
+ # covered by data pixels: 0 outside the data extent, 1 inside
508
+ # (even for bad data), and intermediate values at the edges).
509
+ mask = mcolors ._get_mask (cm ._iterable_variates_in_data (A ))
510
+ if mask .shape == A .shape :
511
+ # we always have to interpolate the mask to account for
512
+ # non-affine transformations
513
+ # To get all pixels where partially covered by the mask
514
+ # we run _resample with an array with np.nan
515
+ nan_mask = np .where (mask , np .float32 (np .nan ), np .float32 (1 ))
516
+ out_alpha = _resample (self , nan_mask , out_shape , t ,
517
+ resample = True )
518
+ else :
519
+ out_alpha = np .ones (out_shape , np .float32 )
520
+ del mask , nan_mask # Make sure we don't use mask anymore!
521
+ out_mask = np .isnan (out_alpha )
522
+ out_alpha [out_mask ] = 1
523
+ # Apply the pixel-by-pixel alpha values if present
524
+ alpha = self .get_alpha ()
525
+ if alpha is not None and np .ndim (alpha ) > 0 :
526
+ out_alpha *= _resample (self , alpha , out_shape ,
527
+ t , resample = True )
528
+ # Resample and norm data
532
529
if self .cmap .n_variates == 1 :
533
- self .nac ._norm [0 ].autoscale_None (A )
534
- A = self .to_rgba (A )
530
+ normed_resampled = self ._resample_and_norm (A ,
531
+ self .nac ._norm [0 ],
532
+ out_shape ,
533
+ out_mask , t )
535
534
else :
536
- A = cm ._iterable_variates_in_data (A )
537
- for n , a in zip (self .nac ._norm , A ):
538
- n .autoscale_None (a )
539
- # We could invoked A = self.to_rgba(A) here
540
- # but that would result in an extra memcopy.
541
- # Instead do norm + cmap() directly.
542
- A = [norm (xx ) for norm , xx in zip (self .nac ._norm , A )]
543
- A = self .cmap (A )
544
- alpha = self ._get_scalar_alpha ()
535
+ normed_resampled = [self ._resample_and_norm (a ,
536
+ self .nac ._norm [i ],
537
+ out_shape ,
538
+ out_mask , t )
539
+ for i , a in
540
+ enumerate (cm ._iterable_variates_in_data (A ))]
541
+ output = self .cmap (normed_resampled , bytes = True )
542
+ # Apply alpha *after* if the input was greyscale without a mask
543
+ alpha_channel = output [:, :, 3 ]
544
+ alpha_channel [:] = ( # Assignment will cast to uint8.
545
+ alpha_channel .astype (np .float32 ) * out_alpha * scalar_alpha )
546
+
547
+ else : # A.ndim == 3
545
548
if A .shape [2 ] == 3 :
546
549
# No need to resample alpha or make a full array; NumPy will expand
547
550
# this out and cast to uint8 if necessary when it's assigned to the
548
551
# alpha channel below.
549
- output_alpha = (255 * alpha ) if A .dtype == np .uint8 else alpha
552
+ output_alpha = (255 * scalar_alpha ) if A .dtype == np .uint8 \
553
+ else scalar_alpha
550
554
else :
551
555
output_alpha = _resample ( # resample alpha channel
552
- self , A [..., 3 ], out_shape , t , alpha = alpha )
556
+ self , A [..., 3 ], out_shape , t , alpha = scalar_alpha )
557
+
553
558
output = _resample ( # resample rgb channels
554
- self , _rgb_to_rgba (A [..., :3 ]), out_shape , t , alpha = alpha )
559
+ self , _rgb_to_rgba (A [..., :3 ]), out_shape , t , alpha = scalar_alpha )
555
560
output [..., 3 ] = output_alpha # recombine rgb and alpha
556
-
557
- # output is now either a 2D array of normed (int or float) data
558
- # or an RGBA array of re-sampled input
559
- output = self .to_rgba (output , bytes = True , norm = False )
561
+ if output .dtype .kind == 'f' :
562
+ output = (output * 255 ).astype (np .uint8 )
560
563
# output is now a correctly sized RGBA array of uint8
561
-
562
- # Apply alpha *after* if the input was greyscale without a mask
563
- if A .ndim == 2 :
564
- alpha = self ._get_scalar_alpha ()
565
- alpha_channel = output [:, :, 3 ]
566
- alpha_channel [:] = ( # Assignment will cast to uint8.
567
- alpha_channel .astype (np .float32 ) * out_alpha * alpha )
568
-
569<
538E
/code>
- else :
564
+ else : # if unsampled:
570
565
if self ._imcache is None :
571
566
self ._imcache = self .to_rgba (A , bytes = True , norm = (A .ndim == 2 ))
572
567
output = self ._imcache
0 commit comments