CropImage
Crop an Image into a smaller image
Welcome to the new Ogre Wiki!
If you haven't done so already, be sure to visit the Wiki Portal to read about how the wiki works. Especially the Ogre Wiki Overview page.
If you haven't done so already, be sure to visit the Wiki Portal to read about how the wiki works. Especially the Ogre Wiki Overview page.
Why Is This Useful?
The ability to obtain arbitrary sections of an image has multiple applications. For example, in a GUI, you might wish to pack your image assets into a single image-- using this code snippet you could then split that image back up into its components.
The Code
/** * Little utility function that crops an image * (Doesn't alter the source image, returns a cropped representation) * * @param source The source image * @param offsetX The X offset from the origin * @param offsetY The Y offset from the origin * @param width The width to crop to * @param height The height to crop to * * @return Returns the cropped representation of the source image if the parameters are valid, * otherwise, returns the source image. */ Ogre::Image cropImage(const Ogre::Image& source, size_t offsetX, size_t offsetY, size_t width, size_t height) { if(offsetX + width > source.getWidth()) return source; else if(offsetY + height > source.getHeight()) return source; size_t bpp = Ogre::PixelUtil::getNumElemBytes(source.getFormat()); const unsigned char *srcData = source.getData(); unsigned char *dstData = new unsigned char[width * height * bpp]; size_t srcPitch = source.getRowSpan(); size_t dstPitch = width * bpp; for(size_t row = 0; row < height; row++) { for(size_t col = 0; col < width * bpp; col++) { dstData[(row * dstPitch) + col] = srcData[((row + offsetY) * srcPitch) + (offsetX * bpp) + col]; } } Ogre::Image croppedImage; croppedImage.loadDynamicImage(dstData, width, height, 1, source.getFormat(), true); return croppedImage; }
--Ajs15822? 07:37, 7 February 2008 (GMT)
Contributors to this page: jacmoe
and
Ajs15822
.
Page last modified on Saturday 02 of January, 2010 05:47:39 GMT by jacmoe
.
The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.
Sidebar
Search box
Online users
60
online users

