VImage VImageManager::scaleImage( VImage src, VImage dest, double factor ) { int nPixel = VImageSize( src ) / VPixelSize( src ); if ( dest == NULL ) { dest = VCreateImage( VImageNBands( src ), VImageNRows( src ), VImageNColumns( src ), VUByteRepn ); } VUByte *destPtr = ( VUByte * ) VImageData( dest ); VShort *srcPtr = ( VShort * ) VImageData( src ); srcPtr = ( VShort * ) VImageData( src ); for ( int i = 0; i < nPixel; i++ ) { if ( *srcPtr <= m_xmin ) *destPtr = 0; else if ( *srcPtr >= m_xmax ) *destPtr = 255; else { int val = *srcPtr - m_xmin; *destPtr = ( VUByte ) ( val * factor ); } srcPtr++; destPtr++; } return dest; }
static VPointer VImageDecodeMethod (VStringConst name, VBundle b) { VImage image; VLong nbands, nrows, ncolumns, pixel_repn; VLong nframes, nviewpoints, ncolors, ncomponents; VAttrList list; size_t length; #define Extract(name, dict, locn, required) \ VExtractAttr (b->list, name, dict, VLongRepn, & locn, required) /* Extract the number of bands, rows, columns, pixel repn, etc.: */ nbands = nframes = nviewpoints = ncolors = ncomponents = 1; /* defaults */ if (! Extract (VNBandsAttr, NULL, nbands, FALSE) || ! Extract (VNRowsAttr, NULL, nrows, TRUE) || ! Extract (VNColumnsAttr, NULL, ncolumns, TRUE) || ! Extract (VRepnAttr, VNumericRepnDict, pixel_repn, TRUE) || ! Extract (VNFramesAttr, NULL, nframes, FALSE) || ! Extract (VNViewpointsAttr, NULL, nviewpoints, FALSE) || ! Extract (VNColorsAttr, NULL, ncolors, FALSE) || ! Extract (VNComponentsAttr, NULL, ncomponents, FALSE)) return NULL; /* Ensure that nbands == nframes * nviewpoints * ncolors * ncomponents. For backwards compatibility, set ncomponents to nbands if nbands != 1 but nframes == nviewpoints == ncolors == ncomponents == 1. */ if (nbands != nframes * nviewpoints * ncolors * ncomponents) { if (nbands != 1 && nframes == 1 && nviewpoints == 1 && ncolors == 1 && ncomponents == 1) ncomponents = nbands; else { VWarning ("VImageDecodeMethod: %s image has inconsistent nbands", name); return NULL; } } /* Create an image with the specified properties: */ if (! (image = VCreateImage ((int) nbands, (int) nrows, (int) ncolumns, (VRepnKind) pixel_repn))) return NULL; VImageNFrames (image) = nframes; VImageNViewpoints (image) = nviewpoints; VImageNColors (image) = ncolors; VImageNComponents (image) = ncomponents; /* Give it whatever attributes remain: */ list = VImageAttrList (image); VImageAttrList (image) = b->list; b->list = list; /* Check that the expected amount of binary data was read: */ length = VImageNPixels (image); if (VPixelRepn (image) == VBitRepn) length = (length + 7) / 8; else length *= VPixelPrecision (image) / 8; if (length != b->length) { VWarning ("VImageDecodeMethod: %s image has wrong data length", name); Fail: VDestroyImage (image); return NULL; } /* Unpack the binary pixel data: */ length = VImageSize (image); if (! VUnpackData (VPixelRepn (image), VImageNPixels (image), b->data, VMsbFirst, & length, & VImageData (image), NULL)) goto Fail; return image; #undef Extract }