# HG changeset patch # User John W. Eaton # Date 1283180700 14400 # Node ID 4c1ffaed8caaf06c7ba6abe7bf3b7b2ff8af4397 # Parent 015ba76371b985274892afaaef27e205f271c317 graphics.cc (convert_cdata): avoid data conversion for entire cdata array at once diff -r 015ba76371b9 -r 4c1ffaed8caa src/ChangeLog --- a/src/ChangeLog Mon Aug 30 11:06:15 2010 +0200 +++ b/src/ChangeLog Mon Aug 30 11:05:00 2010 -0400 @@ -1,3 +1,11 @@ +2010-08-30 John W. Eaton + + * graphics.cc (convert_cdata_1, convert_cdata_2): New functions. + (convert_cdata): Use them to avoid converting all elements of + cdata to double at once. + * graphics.h.in (surface::properties::init): Constrain cdata and + alphadata to be single, double, or uint8. + 2010-08-30 Jaroslav Hajek * graphics.cc (convert_cdata): Avoid extracting pointer to temporary diff -r 015ba76371b9 -r 4c1ffaed8caa src/graphics.cc --- a/src/graphics.cc Mon Aug 30 11:06:15 2010 +0200 +++ b/src/graphics.cc Mon Aug 30 11:05:00 2010 -0400 @@ -552,6 +552,47 @@ while (true); } +static void +convert_cdata_2 (bool is_scaled, double clim_0, double clim_1, + const double *cmapv, double x, octave_idx_type lda, + octave_idx_type nc, octave_idx_type i, double *av) +{ + if (is_scaled) + x = xround ((nc - 1) * (x - clim_0) / (clim_1 - clim_0)); + else + x = xround (x - 1); + + if (xisnan (x)) + { + av[i] = x; + av[i+lda] = x; + av[i+2*lda] = x; + } + else + { + if (x < 0) + x = 0; + else if (x >= nc) + x = (nc - 1); + + octave_idx_type idx = static_cast (x); + + av[i] = cmapv[idx]; + av[i+lda] = cmapv[idx+nc]; + av[i+2*lda] = cmapv[idx+2*nc]; + } +} + +template +void +convert_cdata_1 (bool is_scaled, double clim_0, double clim_1, + const double *cmapv, const T *cv, octave_idx_type lda, + octave_idx_type nc, double *av) +{ + for (octave_idx_type i = 0; i < lda; i++) + convert_cdata_2 (is_scaled, clim_0, clim_1, cmapv, cv[i], lda, nc, i, av); +} + static octave_value convert_cdata (const base_properties& props, const octave_value& cdata, bool is_scaled, int cdim) @@ -598,41 +639,30 @@ double *av = a.fortran_vec (); const double *cmapv = cmap.data (); - const NDArray xcdata = cdata.array_value (); - const double *cv = xcdata.data (); - - if (! error_state) - { - for (octave_idx_type i = 0; i < lda; i++) - { - double x = cv[i]; - - if (is_scaled) - x = xround ((nc - 1) * (x - clim(0)) / (clim(1) - clim(0))); - else - x = xround (x - 1); - - if (xisnan (x)) - { - av[i] = x; - av[i+lda] = x; - av[i+2*lda] = x; - } - else - { - if (x < 0) - x = 0; - else if (x >= nc) - x = (nc - 1); - - octave_idx_type idx = static_cast (x); - - av[i] = cmapv[idx]; - av[i+lda] = cmapv[idx+nc]; - av[i+2*lda] = cmapv[idx+2*nc]; - } - } - } + + double clim_0 = clim(0); + double clim_1 = clim(1); + +#define CONVERT_CDATA_1(ARRAY_T, VAL_FN) \ + do \ + { \ + ARRAY_T tmp = cdata. VAL_FN ## array_value (); \ + \ + convert_cdata_1 (is_scaled, clim_0, clim_1, cmapv, \ + tmp.data (), lda, nc, av); \ + } \ + while (0) + + if (cdata.is_uint8_type ()) + CONVERT_CDATA_1 (uint8NDArray, uint8_); + else if (cdata.is_single_type ()) + CONVERT_CDATA_1 (FloatNDArray, float_); + else if (cdata.is_double_type ()) + CONVERT_CDATA_1 (NDArray, ); + else + error ("unsupported type for cdata (= %s)", cdata.type_name ().c_str ()); + +#undef CONVERT_CDATA_1 return octave_value (a); } diff -r 015ba76371b9 -r 4c1ffaed8caa src/graphics.h.in --- a/src/graphics.h.in Mon Aug 30 11:06:15 2010 +0200 +++ b/src/graphics.h.in Mon Aug 30 11:05:00 2010 -0400 @@ -3710,8 +3710,14 @@ xdata.add_constraint (dim_vector (-1, -1)); ydata.add_constraint (dim_vector (-1, -1)); zdata.add_constraint (dim_vector (-1, -1)); + alphadata.add_constraint ("single"); + alphadata.add_constraint ("double"); + alphadata.add_constraint ("uint8"); alphadata.add_constraint (dim_vector (-1, -1)); vertexnormals.add_constraint (dim_vector (-1, -1, 3)); + cdata.add_constraint ("single"); + cdata.add_constraint ("double"); + cdata.add_constraint ("uint8"); cdata.add_constraint (dim_vector (-1, -1)); cdata.add_constraint (dim_vector (-1, -1, 3)); }