Just had a go at it, since it's really so straight-forward. I suppose the ostream version is actually not really "needed" since the code it encapsulates is pretty much a one-liner itself, so it's more a "for the eye" wrapper than anything else. It's included in this draft, but I could remove it again.
Code: Select all
Index: OgreMain/include/OgreImage.h
===================================================================
RCS file: /cvsroot/ogre/ogrenew/OgreMain/include/OgreImage.h,v
retrieving revision 1.49
diff -u -r1.49 OgreImage.h
--- OgreMain/include/OgreImage.h 31 Aug 2006 22:47:53 -0000 1.49
+++ OgreMain/include/OgreImage.h 4 Sep 2007 15:16:35 -0000
@@ -34,6 +34,8 @@
#include "OgrePixelFormat.h"
#include "OgreDataStream.h"
+#include <iosfwd>
+
namespace Ogre {
enum ImageFlags
@@ -288,6 +290,20 @@
*/
Image & load(DataStreamPtr& stream, const String& type );
+ /** Encode the image to a DataStream.
+ @param
+ type The type you want to encode the image as.
+ */
+ DataStreamPtr encode(const String& type);
+
+ /** Save the image to a std::ostream.
+ @param
+ stream The ostream to write to.
+ @param
+ type The type you want to save the image as.
+ */
+ void save(std::ostream& stream, const String& type);
+
/** Save the image as a file. */
void save(const String& filename);
Code: Select all
Index: OgreMain/src/OgreImage.cpp
===================================================================
RCS file: /cvsroot/ogre/ogrenew/OgreMain/src/OgreImage.cpp,v
retrieving revision 1.64
diff -u -r1.64 OgreImage.cpp
--- OgreMain/src/OgreImage.cpp 2 Jan 2007 16:33:59 -0000 1.64
+++ OgreMain/src/OgreImage.cpp 4 Sep 2007 15:16:20 -0000
@@ -388,7 +388,47 @@
pCodec->codeToFile(wrapper, filename, codeDataPtr);
}
- //-----------------------------------------------------------------------------
+
+ //-----------------------------------------------------------------------------
+ DataStreamPtr Image::encode(const String& type)
+ {
+ if( !m_pBuffer )
+ {
+ OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "No image data loaded",
+ "Image::encode");
+ }
+
+ Codec* pCodec = Codec::getCodec(type);
+ if( !pCodec )
+ OGRE_EXCEPT(
+ Exception::ERR_INVALIDPARAMS,
+ "Unable to encode image - invalid extension.",
+ "Image::encode" );
+
+ ImageCodec::ImageData* imgData = new ImageCodec::ImageData();
+ imgData->format = m_eFormat;
+ imgData->height = m_uHeight;
+ imgData->width = m_uWidth;
+ imgData->depth = m_uDepth;
+ // Wrap in CodecDataPtr, this will delete
+ Codec::CodecDataPtr codeDataPtr(imgData);
+ // Wrap memory, be sure not to delete when stream destroyed
+ MemoryDataStreamPtr wrapper(new MemoryDataStream(m_pBuffer, m_uSize, false));
+
+ return pCodec->code(wrapper, codeDataPtr);
+ }
+
+ //-----------------------------------------------------------------------------
+ void Image::save(std::ostream& stream, const String& type)
+ {
+ // encode to memory
+ DataStreamPtr data = encode(type);
+
+ // write results to the stream
+ stream << data->getAsString();
+ }
+
+ //-----------------------------------------------------------------------------
Image & Image::load(DataStreamPtr& stream, const String& type )
{
if( m_pBuffer && m_bAutoDelete )
If that's acceptable, I'll submit it properly.
BTW, since I'm just touching the Image class, I have two more "issues" with the load functions. For one, the two load functions (one taking name and group to load from the resource system, the other to load from a DataStream) share a lot of code - actually I think the former should call the other, since it retrieves a DataStream from the ResourceGroupManager.
And then, correct me if I'm wrong, but I had the impression that whenever I access a resource from Ogre's resource system, specifying the group name was optional. If the group name is omitted, it would just search the standard group first and then extend the search to other groups. However, the Image load function is an exception since the group parameter has no default value. Is this intentional, or could the group parameter get a default value of ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME? Again no big issue, I just stumble over it occasionally
