Mercurial > hg > octave-terminal
changeset 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 | f56255a2b890 |
files | src/ChangeLog src/graphics.cc src/graphics.h.in |
diffstat | 3 files changed, 79 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2010-08-30 John W. Eaton <jwe@octave.org> + + * 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 <highegg@gmail.com> * graphics.cc (convert_cdata): Avoid extracting pointer to temporary
--- a/src/graphics.cc +++ b/src/graphics.cc @@ -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<octave_idx_type> (x); + + av[i] = cmapv[idx]; + av[i+lda] = cmapv[idx+nc]; + av[i+2*lda] = cmapv[idx+2*nc]; + } +} + +template <class T> +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<octave_idx_type> (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); }
--- a/src/graphics.h.in +++ b/src/graphics.h.in @@ -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)); }