OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: CropImage
View page
Source of version: 3
(current)
!!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 {CODE(wrap="1", colors="c++")}/** * 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; }{CODE} --((User:Ajs15822|Ajs15822)) 07:37, 7 February 2008 (GMT)
Search by Tags
Search Wiki by Freetags
Latest Changes
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
...more
Search
Find
Advanced
Search Help
Online Users
129 online users