texture.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <rve_render_server/texture.h>
00031 #include <resource_retriever/retriever.h>
00032 
00033 #include <sensor_msgs/Image.h>
00034 #include <sensor_msgs/image_encodings.h>
00035 
00036 #include <OGRE/OgreTextureManager.h>
00037 
00038 #include <boost/filesystem.hpp>
00039 
00040 namespace fs = boost::filesystem;
00041 
00042 namespace rve_render_server
00043 {
00044 
00045 Texture::Texture(const rve_common::UUID& id, uint32_t num_mip_maps)
00046 : id_(id)
00047 , num_mip_maps_(num_mip_maps)
00048 {
00049   int usage = Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE|Ogre::TU_AUTOMIPMAP;
00050   Ogre::PixelFormat format = Ogre::TextureManager::getSingleton().getNativeFormat(Ogre::TEX_TYPE_2D, Ogre::PF_BYTE_RGBA, usage);
00051   texture_ = Ogre::TextureManager::getSingleton().createManual(getID().toString(), ROS_PACKAGE_NAME, Ogre::TEX_TYPE_2D, 1, 1, num_mip_maps, format, usage);
00052 }
00053 
00054 Texture::~Texture()
00055 {
00056   Ogre::TextureManager::getSingleton().remove(id_.toString());
00057 }
00058 
00059 void Texture::setImage(const sensor_msgs::Image& image)
00060 {
00061   if (image.data.empty())
00062   {
00063     throw std::runtime_error("Image has no data");
00064   }
00065 
00066   Ogre::PixelFormat format = Ogre::PF_R8G8B8;
00067   Ogre::Image ogre_image;
00068   std::vector<uint8_t> buffer;
00069   void* data_ptr = (void*)&image.data[0];
00070   uint32_t data_size = image.data.size();
00071   if (image.encoding == sensor_msgs::image_encodings::RGB8)
00072   {
00073     format = Ogre::PF_BYTE_RGB;
00074   }
00075   else if (image.encoding == sensor_msgs::image_encodings::RGBA8)
00076   {
00077     format = Ogre::PF_BYTE_RGBA;
00078   }
00079   else if (image.encoding == sensor_msgs::image_encodings::TYPE_8UC4 ||
00080            image.encoding == sensor_msgs::image_encodings::TYPE_8SC4 ||
00081            image.encoding == sensor_msgs::image_encodings::BGRA8)
00082   {
00083     format = Ogre::PF_BYTE_BGRA;
00084   }
00085   else if (image.encoding == sensor_msgs::image_encodings::TYPE_8UC3 ||
00086            image.encoding == sensor_msgs::image_encodings::TYPE_8SC3 ||
00087            image.encoding == sensor_msgs::image_encodings::BGR8)
00088   {
00089     format = Ogre::PF_BYTE_BGR;
00090   }
00091   else if (image.encoding == sensor_msgs::image_encodings::TYPE_8UC1 ||
00092            image.encoding == sensor_msgs::image_encodings::TYPE_8SC1 ||
00093            image.encoding == sensor_msgs::image_encodings::MONO8)
00094   {
00095     format = Ogre::PF_BYTE_L;
00096   }
00097   else if (image.encoding == sensor_msgs::image_encodings::TYPE_16UC1 ||
00098            image.encoding == sensor_msgs::image_encodings::TYPE_16SC1 ||
00099            image.encoding == sensor_msgs::image_encodings::MONO16)
00100   {
00101     format = Ogre::PF_BYTE_L;
00102 
00103     // downsample manually to 8-bit, because otherwise the lower 8-bits are simply removed
00104     buffer.resize(image.data.size() / 2);
00105     data_size = buffer.size();
00106     data_ptr = (void*)&buffer[0];
00107     for (size_t i = 0; i < data_size; ++i)
00108     {
00109       uint16_t s = image.data[2*i] << 8 | image.data[2*i + 1];
00110       float val = (float)s / std::numeric_limits<uint16_t>::max();
00111       buffer[i] = val * 255;
00112     }
00113   }
00114   else if (image.encoding.find("bayer") == 0)
00115   {
00116     format = Ogre::PF_BYTE_L;
00117   }
00118   else
00119   {
00120     throw std::runtime_error("Unsupported image encoding [" + image.encoding + "]");
00121   }
00122 
00123   // TODO: Support different steps/strides
00124 
00125   Ogre::DataStreamPtr pixel_stream;
00126   pixel_stream.bind(new Ogre::MemoryDataStream(data_ptr, data_size));
00127   ogre_image.loadRawData(pixel_stream, image.width, image.height, format);
00128 
00129   texture_->unload();
00130   texture_->loadImage(ogre_image);
00131 }
00132 
00133 void Texture::loadFromFile(const std::string& file)
00134 {
00135   resource_retriever::Retriever retriever;
00136   resource_retriever::MemoryResource res = retriever.get(file);
00137 
00138   if (res.size == 0)
00139   {
00140     throw std::runtime_error("Zero-length file [" + file + "]");
00141   }
00142 
00143   Ogre::DataStreamPtr pixel_stream;
00144   pixel_stream.bind(new Ogre::MemoryDataStream(res.data.get(), res.size));
00145   Ogre::Image image;
00146   image.load(pixel_stream, fs::extension(file).substr(1));
00147 
00148   texture_->unload();
00149   texture_->loadImage(image);
00150 }
00151 
00152 }


rve_render_server
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:15