/* See float_array_from_uint16_image.h */ /* Last edited on 2019-08-30 07:22:33 by jstolfi */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define NA float_array_NAXES /* Number of dimensions. */ float_array_t *float_array_from_uint16_image ( uint16_image_t *img, bool_t isMask, double lo[], double hi[], bool_t yrev, bool_t verbose ) { /* Get image dimensions: */ int NX = img->cols; int NY = img->rows; /* Channel counts: */ int NC = img->chns; /* Num of channels. */ /* Allocate float image: */ ix_dim_t na = 3; ix_size_t sz[na]; sz[0] = NC; sz[1] = NX; sz[2] = NY; float_array_t AN = float_array_new(na, sz); float_array_t *A = &AN; /* Max sample value in integer image: */ uint16_t maxval = img->maxval; /* Input and output range registers: */ sample_uint32_t imin[NC], imax[NC]; /* Input range registers. */ float vmin[NC], vmax[NC]; /* Output range registers. */ for (int c = 0; c < NC; c++) { imin[c] = maxval; imax[c] = 0; vmin[c] = +INFINITY; vmax[c] = -INFINITY; } /* Convert pixels, keep statistics: */ ix_index_t ix[na]; for(int fy = 0; fy < NY; fy++) { /* Fill array plane {ix[2]==fy}: */ int iy = (yrev ? NY - 1 - fy : fy); /* Row index in image array. */ uint16_t *prow = img->smp[iy]; for(int x = 0; x < NX; x++) { for (int c = 0; c < NC; c++) { /* Convert int sample {*prow} to float {v}, store, keep stats: */ sample_uint32_t ismp = (*prow); double loc = (lo == NULL ? 0.0 : lo[c]); double hic = (hi == NULL ? 1.0 : hi[c]); float fsmp = sample_conv_floatize ( ismp, maxval, isMask, loc, hic, &(imin[c]), &(imax[c]), &(vmin[c]), &(vmax[c]) ); ix[0] = c; ix[1] = x; ix[2] = fy; ix[3] = ix[4] = ix[5] = 0; float_array_set_elem(A, ix, fsmp); prow++; } } } if (verbose) { /* Print statistics: */ long int NPIX = ((long int)NX)*((long int)NY); fprintf(stderr, " %ld pixels in PNM image\n", NPIX); if (NPIX > 0) { for (int c = 0; c < NC; c++) { double loc = (lo == NULL ? 0.0 : lo[c]); double hic = (hi == NULL ? 1.0 : hi[c]); sample_conv_print_floatize_stats ( c, c, imin[c], imax[c], maxval, loc, hic, vmin[c], vmax[c] ); } } } return A; }