bool ImageWriter::WriteJPG(const std::string& sFilename, ImageObject& image, int quality)
{
   unsigned char* pInput = 0;
   ImageObject rgbimage;

   if (image.GetPixelFormat() != Img::PixelFormat_RGB)
   {
      image.Convert(Img::PixelFormat_RGB, rgbimage);
      pInput = rgbimage.GetRawData().get();
   }
   else
   {
      pInput = image.GetRawData().get();
   }

   boost::shared_array<unsigned char> outjpg;
   int len;
   if (JPEGHandler::RGBToJpeg(pInput, image.GetWidth(), image.GetHeight(), quality, outjpg, len))
   {
      std::fstream off(sFilename.c_str(), std::ios::out | std::ios::binary);
      if (off.good())
      {
         off.write((char*)outjpg.get(), (std::streamsize)len);
         off.close();
         return true;
      }
   }

   return false;

}