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; }
VBoolean VFillImage (VImage image, VBand band, VDoublePromoted value) { int i, npixels; VPointer first_pixel; #define Fill(type) \ { \ type d = value, *pp = first_pixel; \ for (i = npixels; i > 0; i--) \ *pp++ = d; \ } /* Locate the specified band(s) in the image: */ if (! VSelectBand ("VFillImage", image, band, & npixels, & first_pixel)) return FALSE; /* To avoid surprises when filling integer pixels, round the fill value to the nearest integer: */ if (VIsIntegerRepn (VPixelRepn (image))) value += (value > 0.0) ? 0.5 : -0.5; /* The memset() routine is probably the fastest way of filling memory, but it can only be used here in certain situations. We only check for these, the most straightforward of the situations: (a) pixel values occupy a byte (b) the value to be filled is all 0's It is when the value to be filled is all 0's and the pixel representation is floating point that we take advantage of the assumption that 0.0 is represented as all-bits-zero. */ if (VPixelSize (image) == 1 || value == 0.0) memset (first_pixel, (int) value, npixels * VPixelSize (image)); /* Otherwise, fill by looping over all pixels: */ else switch (VPixelRepn (image)) { case VBitRepn: Fill (VBit); break; case VUByteRepn: Fill (VUByte); break; case VSByteRepn: Fill (VSByte); break; case VShortRepn: Fill (VShort); break; case VLongRepn: Fill (VLong); break; case VFloatRepn: Fill (VFloat); break; case VDoubleRepn: Fill (VDouble); break; default: break; } return TRUE; }
VBoolean VCopyBand (VImage src, VBand src_band, VImage dest, VBand dest_band) { int nbands, src_npixels, dest_npixels; VPointer src_pixels, dest_pixels; /* The destination image must exist: */ if (! dest) { VWarning ("VCopyBand: No destination specified"); return FALSE; } /* VAllBands not accepted for destination band: */ if (dest_band < 0 || dest_band >= VImageNBands (dest)) { VWarning ("VCopyBand: Band %d referenced in image of %d bands", (int) dest_band, (int) VImageNBands (dest)); return FALSE; } /* Ensure that the destination image has the appropriate dimensions and pixel representation: */ nbands = dest_band; if (src_band == VAllBands) nbands += VImageNBands (src) - 1; if (nbands < VImageNBands (dest)) nbands = VImageNBands (dest); if (! VSelectDestImage ("VCopyBand", dest, nbands, VImageNRows (src), VImageNColumns (src), VPixelRepn (src))) return FALSE; /* Locate the specified source and destination bands: */ if (! VSelectBand ("VCopyBand", src, src_band, & src_npixels, & src_pixels)) return FALSE; if (! VSelectBand ("VCopyBand", dest, dest_band, & dest_npixels, & dest_pixels)) return FALSE; /* Copy from the source band to the destination band: */ memcpy (dest_pixels, src_pixels, src_npixels * VPixelSize (src)); return TRUE; }
VImage VCopyImagePixels (VImage src, VImage dest, VBand band) { int npixels; VPointer src_pixels; VImage result; /* Locate the source and destination of the copy: */ if (! VSelectBand ("VCopyImagePixels", src, band, & npixels, & src_pixels)) return NULL; result = VSelectDestImage ("VCopyImagePixels", dest, band == VAllBands ? VImageNBands (src) : 1, VImageNRows (src), VImageNColumns (src), VPixelRepn (src)); if (! result) return NULL; /* Copy pixel values from src to dest: */ memcpy (VImageData (result), src_pixels, npixels * VPixelSize (src)); return result; }