comparison src/graphics.cc @ 10923:4c1ffaed8caa

graphics.cc (convert_cdata): avoid data conversion for entire cdata array at once
author John W. Eaton <jwe@octave.org>
date Mon, 30 Aug 2010 11:05:00 -0400
parents 015ba76371b9
children 97294dd3ccd4
comparison
equal deleted inserted replaced
10922:015ba76371b9 10923:4c1ffaed8caa
550 return graphics_object (); 550 return graphics_object ();
551 } 551 }
552 while (true); 552 while (true);
553 } 553 }
554 554
555 static void
556 convert_cdata_2 (bool is_scaled, double clim_0, double clim_1,
557 const double *cmapv, double x, octave_idx_type lda,
558 octave_idx_type nc, octave_idx_type i, double *av)
559 {
560 if (is_scaled)
561 x = xround ((nc - 1) * (x - clim_0) / (clim_1 - clim_0));
562 else
563 x = xround (x - 1);
564
565 if (xisnan (x))
566 {
567 av[i] = x;
568 av[i+lda] = x;
569 av[i+2*lda] = x;
570 }
571 else
572 {
573 if (x < 0)
574 x = 0;
575 else if (x >= nc)
576 x = (nc - 1);
577
578 octave_idx_type idx = static_cast<octave_idx_type> (x);
579
580 av[i] = cmapv[idx];
581 av[i+lda] = cmapv[idx+nc];
582 av[i+2*lda] = cmapv[idx+2*nc];
583 }
584 }
585
586 template <class T>
587 void
588 convert_cdata_1 (bool is_scaled, double clim_0, double clim_1,
589 const double *cmapv, const T *cv, octave_idx_type lda,
590 octave_idx_type nc, double *av)
591 {
592 for (octave_idx_type i = 0; i < lda; i++)
593 convert_cdata_2 (is_scaled, clim_0, clim_1, cmapv, cv[i], lda, nc, i, av);
594 }
595
555 static octave_value 596 static octave_value
556 convert_cdata (const base_properties& props, const octave_value& cdata, 597 convert_cdata (const base_properties& props, const octave_value& cdata,
557 bool is_scaled, int cdim) 598 bool is_scaled, int cdim)
558 { 599 {
559 dim_vector dv (cdata.dims ()); 600 dim_vector dv (cdata.dims ());
596 octave_idx_type lda = a.numel () / static_cast<octave_idx_type> (3); 637 octave_idx_type lda = a.numel () / static_cast<octave_idx_type> (3);
597 octave_idx_type nc = cmap.rows (); 638 octave_idx_type nc = cmap.rows ();
598 639
599 double *av = a.fortran_vec (); 640 double *av = a.fortran_vec ();
600 const double *cmapv = cmap.data (); 641 const double *cmapv = cmap.data ();
601 const NDArray xcdata = cdata.array_value (); 642
602 const double *cv = xcdata.data (); 643 double clim_0 = clim(0);
603 644 double clim_1 = clim(1);
604 if (! error_state) 645
605 { 646 #define CONVERT_CDATA_1(ARRAY_T, VAL_FN) \
606 for (octave_idx_type i = 0; i < lda; i++) 647 do \
607 { 648 { \
608 double x = cv[i]; 649 ARRAY_T tmp = cdata. VAL_FN ## array_value (); \
609 650 \
610 if (is_scaled) 651 convert_cdata_1 (is_scaled, clim_0, clim_1, cmapv, \
611 x = xround ((nc - 1) * (x - clim(0)) / (clim(1) - clim(0))); 652 tmp.data (), lda, nc, av); \
612 else 653 } \
613 x = xround (x - 1); 654 while (0)
614 655
615 if (xisnan (x)) 656 if (cdata.is_uint8_type ())
616 { 657 CONVERT_CDATA_1 (uint8NDArray, uint8_);
617 av[i] = x; 658 else if (cdata.is_single_type ())
618 av[i+lda] = x; 659 CONVERT_CDATA_1 (FloatNDArray, float_);
619 av[i+2*lda] = x; 660 else if (cdata.is_double_type ())
620 } 661 CONVERT_CDATA_1 (NDArray, );
621 else 662 else
622 { 663 error ("unsupported type for cdata (= %s)", cdata.type_name ().c_str ());
623 if (x < 0) 664
624 x = 0; 665 #undef CONVERT_CDATA_1
625 else if (x >= nc)
626 x = (nc - 1);
627
628 octave_idx_type idx = static_cast<octave_idx_type> (x);
629
630 av[i] = cmapv[idx];
631 av[i+lda] = cmapv[idx+nc];
632 av[i+2*lda] = cmapv[idx+2*nc];
633 }
634 }
635 }
636 666
637 return octave_value (a); 667 return octave_value (a);
638 } 668 }
639 669
640 template<class T> 670 template<class T>