当前位置: 首页>>代码示例>>C++>>正文


C++ osg::Image类代码示例

本文整理汇总了C++中osg::Image的典型用法代码示例。如果您正苦于以下问题:C++ Image类的具体用法?C++ Image怎么用?C++ Image使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Image类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: writeImage

 virtual WriteResult writeImage(const osg::Image& img,std::ostream& fout,const osgDB::ReaderWriter::Options *options) const
 {
     osg::ref_ptr<osg::Image> tmp_img = new osg::Image(img);
     tmp_img->flipVertical();
     WriteResult::WriteStatus ws = write_JPEG_file(fout,img.s(),img.t(),(JSAMPLE*)(tmp_img->data()),getQuality(options));
     return ws;
 }
开发者ID:joevandyk,项目名称:osg,代码行数:7,代码来源:ReaderWriterJPEG.cpp

示例2: writeImage

        virtual WriteResult writeImage(const osg::Image& image,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
        {
            // Only ppm format output supported
            std::string ext = osgDB::getFileExtension(fileName);
            if ( !osgDB::equalCaseInsensitive(ext, "ppm") ) return WriteResult::FILE_NOT_HANDLED;
            
            // only support rgb images right now.
            if (image.getPixelFormat()!=GL_RGB || image.getDataType()!=GL_UNSIGNED_BYTE) return WriteResult("Error image pixel format not supported by pnm writer.");

            osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
            if(!fout) return WriteResult::ERROR_IN_WRITING_FILE;

            return writeImage(image,fout,options);
        }
开发者ID:aalex,项目名称:osg,代码行数:14,代码来源:ReaderWriterPNM.cpp

示例3: switch

// create the osg image from the given format
bool NVTTProcessor::OSGImageOutputHandler::assignImage(osg::Image& image)
{
    // convert nvtt format to OpenGL pixel format
    GLint pixelFormat;
    switch (_format)
    {
    case nvtt::Format_RGBA:
        pixelFormat = _discardAlpha ? GL_RGB : GL_RGBA;
        break;
    case nvtt::Format_DXT1:
        pixelFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
        break;
    case nvtt::Format_DXT1a:
        pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
        break;
    case nvtt::Format_DXT3:
        pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
        break;
    case nvtt::Format_DXT5:
        pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
        break;
    default:
        OSG_WARN<<" Invalid or not supported format"<<std::endl;
        return false;
    }

    // Compute the total size and the mipmap offsets
    osg::Image::MipmapDataType mipmapOffsets(_mipmaps.size()-1);
    unsigned int totalSize = _mipmaps[0]->size();
    for (unsigned int n=1; n<_mipmaps.size(); n++)
    {
        mipmapOffsets[n-1] = totalSize;
        totalSize += _mipmaps[n]->size();
    }

    // Allocate data and copy it
    unsigned char* data = new unsigned char[ totalSize ];
    unsigned char* ptr = data;
    for (unsigned int n=0; n<_mipmaps.size(); n++)
    {
        memcpy( ptr, &(*_mipmaps[n])[0], _mipmaps[n]->size() );
        ptr += _mipmaps[n]->size();
    }

    image.setImage(_width,_height,1,pixelFormat,pixelFormat,GL_UNSIGNED_BYTE,data,osg::Image::USE_NEW_DELETE);
    image.setMipmapLevels(mipmapOffsets);

    return true;
}
开发者ID:Kurdakov,项目名称:emscripten_OSG,代码行数:50,代码来源:NVTTImageProcessor.cpp

示例4: writeImage

        virtual WriteResult writeImage(const osg::Image& img,std::ostream& fout,const osgDB::ReaderWriter::Options*) const
        {
            if (img.isCompressed())
            {
                OSG_NOTICE<<"Warning: RGB plugin does not supporting writing compressed imagery."<<std::endl;
                return WriteResult::ERROR_IN_WRITING_FILE;
            }
            if (!img.isDataContiguous())
            {
                OSG_NOTICE<<"Warning: RGB plugin does not supporting writing non contiguous imagery."<<std::endl;
                return WriteResult::ERROR_IN_WRITING_FILE;
            }

            return writeRGBStream(img,fout,"");
        }
开发者ID:yueying,项目名称:osg,代码行数:15,代码来源:ReaderWriterRGB.cpp

示例5: convertRGBToBGRA

// Convert RGB to BGRA : nvtt only accepts BGRA pixel format
void NVTTProcessor::convertRGBToBGRA( std::vector<unsigned char>& outputData, const osg::Image& image )
{
    unsigned int n=0;
    for(int row=0; row<image.t(); ++row)
    {
        const unsigned char* data = image.data(0,row);
        for(int column=0; column<image.s(); ++column)
        {
            outputData[n] = data[column*3+2];
            outputData[n+1] = data[column*3+1];
            outputData[n+2] = data[column*3];
            outputData[n+3] = 255;
            n+=4;
        }
    }
}
开发者ID:Kurdakov,项目名称:emscripten_OSG,代码行数:17,代码来源:NVTTImageProcessor.cpp

示例6: compress

void NVTTProcessor::compress(osg::Image& image, osg::Texture::InternalFormatMode compressedFormat, bool generateMipMap, bool resizeToPowerOfTwo, CompressionMethod method, CompressionQuality quality)
{
    nvtt::Format format;
    switch (compressedFormat)
    {
    case osg::Texture::USE_S3TC_DXT1_COMPRESSION:
        if (image.getPixelFormat() == GL_RGBA)
            format = nvtt::Format_DXT1a;
        else
            format = nvtt::Format_DXT1;
        break;
    case osg::Texture::USE_S3TC_DXT1c_COMPRESSION:
        format = nvtt::Format_DXT1;
        break;
    case osg::Texture::USE_S3TC_DXT1a_COMPRESSION:
        format = nvtt::Format_DXT1a;
        break;
    case osg::Texture::USE_S3TC_DXT3_COMPRESSION:
        format = nvtt::Format_DXT3;
        break;
    case osg::Texture::USE_S3TC_DXT5_COMPRESSION:
        format = nvtt::Format_DXT5;
        break;
    default:
        OSG_WARN<<" Invalid or not supported compress format"<<std::endl;
        return;
    }

    process( image, format, generateMipMap, resizeToPowerOfTwo, method, quality );
}
开发者ID:Kurdakov,项目名称:emscripten_OSG,代码行数:30,代码来源:NVTTImageProcessor.cpp

示例7: writeImage

  virtual WriteResult writeImage(const osg::Image &img, const std::string &fileName, const osgDB::ReaderWriter::Options *options) const
  {
    std::string ext = osgDB::getFileExtension(fileName);
    if (!acceptsExtension(ext))
      return WriteResult::FILE_NOT_HANDLED;

    int internalFormat = osg::Image::computeNumComponents(img.getPixelFormat());

    osgDB::ofstream f(fileName.c_str(), std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
    if (!f)
      return WriteResult::ERROR_IN_WRITING_FILE;

    return writeImage(img, f, options);
  }
开发者ID:emminizer,项目名称:osgearth,代码行数:14,代码来源:ReaderWriterWebP.cpp

示例8: restore

void DepthCallback::restore(osg::Image& src,string& filename,int w,int h) const
{
	float * tmp= reinterpret_cast<float*>(src.data());
	fstream fout;
	fout.open(filename,ios::out);
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			fout<<tmp[i*w+j]<<" ";
		}
		fout<<endl;
	}
	fout.close();
}
开发者ID:Jeff885,项目名称:Depth_Compression,代码行数:15,代码来源:DepthCallback.cpp

示例9: display

void display(void)
{
    OSG::Matrix m1;

    // Anim
    OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME);
    m1.setTransform(-sceneTrans, OSG::Quaternion(OSG::Vec3f(0,1,0), 
                                                 t / 1000.f));
    sceneXform->setMatrix(m1);

    OSG::commitChanges();

    mgr->redraw();

    // all done, swap
    glutSwapBuffers();

    if(bReadBack == true)
    {
        OSG::Image *pImg = tx1o->getImage();
        
        pImg->write("/tmp/foo.png");
    }
}
开发者ID:Himbeertoni,项目名称:OpenSGDevMaster,代码行数:24,代码来源:testSimpleStage.cpp

示例10: operator

    void operator()(const osg::Image& image, const unsigned int)
    {
        frame_id++;

        QImage::Format qtFormat;
        if (image.getPixelFormat() == GL_BGR)
            qtFormat = QImage::Format_RGB888;
        else if (image.getPixelFormat() == GL_BGRA)
            qtFormat = QImage::Format_ARGB32;
        else if (image.getPixelFormat() == GL_RGB)
            qtFormat = QImage::Format_RGB888;
        else if (image.getPixelFormat() == GL_RGBA)
            qtFormat = QImage::Format_ARGB32;
        else
            throw std::runtime_error("cannot interpret osg-provided image format " +
                    boost::lexical_cast<std::string>(image.getPixelFormat()));

        this->image = QImage(image.data(), image.s(), image.t(), qtFormat);
    }
开发者ID:arneboe,项目名称:gui-vizkit3d,代码行数:19,代码来源:Vizkit3DWidget.cpp

示例11: CreateCGImageFromOSGData

/* Create a CGImageRef from osg::Image.
 * Code adapted from
 * http://developer.apple.com/samplecode/OpenGLScreenSnapshot/listing2.html
 */
CGImageRef CreateCGImageFromOSGData(const osg::Image& osg_image)
{
    size_t image_width = osg_image.s();
    size_t image_height = osg_image.t();
    /* From Apple's header for CGBitmapContextCreate()
     * Each row of the bitmap consists of `bytesPerRow' bytes, which must be at
     * least `(width * bitsPerComponent * number of components + 7)/8' bytes.
     */
    size_t target_bytes_per_row;

    CGColorSpaceRef color_space;
    CGBitmapInfo bitmap_info;
    /* From what I can figure out so far...
     * We need to create a CGContext connected to the data we want to save
     * and then call CGBitmapContextCreateImage() on that context to get
     * a CGImageRef.
     * However, OS X only allows 4-component image formats (e.g. RGBA) and not
     * just RGB for the RGB-based CGContext. So for a 24-bit image coming in,
     * we need to expand the data to 32-bit.
     * The easiest and fastest way to do that is through the vImage framework
     * which is part of the Accelerate framework.
     * Also, the osg::Image data coming in is inverted from what we want, so
     * we need to invert the image too. Since the osg::Image is const,
     * we don't want to touch the data, so again we turn to the vImage framework
     * and invert the data.
     */
    vImage_Buffer vimage_buffer_in =
    {
        (void*)osg_image.data(), // need to override const, but we don't modify the data so it's safe
        image_height,
        image_width,
        osg_image.getRowSizeInBytes()
    };

    void* out_image_data;
    vImage_Buffer vimage_buffer_out =
    {
        NULL, // will fill-in in switch
        image_height,
        image_width,
        0 // will fill-in in switch
    };
    vImage_Error vimage_error_flag;

    // FIXME: Do I want to use format, type, or internalFormat?
    switch(osg_image.getPixelFormat())
    {
        case GL_LUMINANCE:
        {
            bitmap_info = kCGImageAlphaNone;
            target_bytes_per_row = (image_width * 8 + 7)/8;
            //color_space = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);
            color_space = CGColorSpaceCreateDeviceGray();
            if(NULL == color_space)
            {
                return NULL;
            }

            //    out_image_data = calloc(target_bytes_per_row, image_height);
            out_image_data = malloc(target_bytes_per_row * image_height);
            if(NULL == out_image_data)
            {
                OSG_WARN << "In CreateCGImageFromOSGData, malloc failed" << std::endl;
                CGColorSpaceRelease(color_space);
                return NULL;
            }

            vimage_buffer_out.data = out_image_data;
            vimage_buffer_out.rowBytes = target_bytes_per_row;

            // Now invert the image
            vimage_error_flag = vImageVerticalReflect_Planar8(
                &vimage_buffer_in, // since the osg_image is const...
                &vimage_buffer_out, // don't reuse the buffer
                kvImageNoFlags
            );
            if(vimage_error_flag != kvImageNoError)
            {
                OSG_WARN << "In CreateCGImageFromOSGData for GL_LUMINANCE, vImageVerticalReflect_Planar8 failed with vImage Error Code: " << vimage_error_flag << std::endl;
                free(out_image_data);
                CGColorSpaceRelease(color_space);
                return NULL;
            }


            break;
        }
        case GL_ALPHA:
        {
            bitmap_info = kCGImageAlphaOnly;
            target_bytes_per_row = (image_width * 8 + 7)/8;
            // According to:
            // http://developer.apple.com/qa/qa2001/qa1037.html
            // colorSpace=NULL is for alpha only
            color_space = NULL;

//.........这里部分代码省略.........
开发者ID:BodyViz,项目名称:osg,代码行数:101,代码来源:ReaderWriterImageIO.cpp

示例12: writeTIFStream

        WriteResult::WriteStatus writeTIFStream(std::ostream& fout, const osg::Image& img, const osgDB::ReaderWriter::Options* options) const
        {
            int compressionType = COMPRESSION_PACKBITS;
            if (options) {
                std::istringstream iss(options->getOptionString());
                std::string opt;
                while (iss >> opt) {
                    opt = osgDB::convertToLowerCase(opt);

                    std::size_t eqInd = opt.find("=");
                    if (opt.substr(0, eqInd) == "tiff_compression") {
                        std::string compressTypeOpt;
                        compressTypeOpt = opt.substr(eqInd + 1);
                        compressTypeOpt = osgDB::convertToLowerCase(compressTypeOpt);
                        if (compressTypeOpt == "packbits") {
                            compressionType = COMPRESSION_PACKBITS;
                        }
                        else if (compressTypeOpt == "lzw") {
                            compressionType = COMPRESSION_LZW;
                        }
                        else if (compressTypeOpt == "jpeg") {
                            compressionType = COMPRESSION_JPEG;
                        }
                    }
                }
            }


            //Code is based from the following article on CodeProject.com
            //http://www.codeproject.com/bitmap/BitmapsToTiffs.asp

            TIFF *image;
            int samplesPerPixel;
            int bitsPerSample;
            uint16 photometric;

            image = TIFFClientOpen("outputstream", "w", (thandle_t)&fout,
                                    libtiffOStreamReadProc, //Custom read function
                                    libtiffOStreamWriteProc, //Custom write function
                                    libtiffOStreamSeekProc, //Custom seek function
                                    libtiffStreamCloseProc, //Custom close function
                                    libtiffOStreamSizeProc, //Custom size function
                                    libtiffStreamMapProc, //Custom map function
                                    libtiffStreamUnmapProc); //Custom unmap function

            if(image == NULL)
            {
                return WriteResult::ERROR_IN_WRITING_FILE;
            }

            switch(img.getPixelFormat()) {
                case GL_DEPTH_COMPONENT:
                case GL_LUMINANCE:
                case GL_ALPHA:
                    photometric = PHOTOMETRIC_MINISBLACK;
                    samplesPerPixel = 1;
                    break;
                case GL_LUMINANCE_ALPHA:
                    photometric = PHOTOMETRIC_MINISBLACK;
                    samplesPerPixel = 2;
                    break;
                case GL_RGB:
                    photometric = PHOTOMETRIC_RGB;
                    samplesPerPixel = 3;
                    break;
                case GL_RGBA:
                    photometric = PHOTOMETRIC_RGB;
                    samplesPerPixel = 4;
                    break;
                default:
                    return WriteResult::ERROR_IN_WRITING_FILE;
                    break;
            }

            switch(img.getDataType()){
                case GL_FLOAT:
                    TIFFSetField(image, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
                    TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, 1);
                    bitsPerSample = 32;
                    break;
                case GL_SHORT:
                    TIFFSetField(image, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
                    bitsPerSample = 16;
                    break;
                default:
                    bitsPerSample = 8;
                    break;
            }

            TIFFSetField(image, TIFFTAG_IMAGEWIDTH,img.s());
            TIFFSetField(image, TIFFTAG_IMAGELENGTH,img.t());
            TIFFSetField(image, TIFFTAG_BITSPERSAMPLE,bitsPerSample);
            TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL,samplesPerPixel);
            TIFFSetField(image, TIFFTAG_PHOTOMETRIC, photometric);
            TIFFSetField(image, TIFFTAG_COMPRESSION, compressionType);
            TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
            TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

            //uint32 rowsperstrip = TIFFDefaultStripSize(image, -1);
            //TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
//.........这里部分代码省略.........
开发者ID:4ker,项目名称:osg,代码行数:101,代码来源:ReaderWriterTIFF.cpp

示例13: writeTIFStream

        WriteResult::WriteStatus writeTIFStream(std::ostream& fout, const osg::Image& img) const
        {
            //Code is based from the following article on CodeProject.com
            //http://www.codeproject.com/bitmap/BitmapsToTiffs.asp

            TIFF *image;
            int samplesPerPixel;
            int bitsPerSample;
            uint16 photometric;

            image = TIFFClientOpen("outputstream", "w", (thandle_t)&fout,
                                    libtiffOStreamReadProc, //Custom read function
                                    libtiffOStreamWriteProc, //Custom write function
                                    libtiffOStreamSeekProc, //Custom seek function
                                    libtiffStreamCloseProc, //Custom close function
                                    libtiffOStreamSizeProc, //Custom size function
                                    libtiffStreamMapProc, //Custom map function
                                    libtiffStreamUnmapProc); //Custom unmap function
            
            if(image == NULL)
            {
                return WriteResult::ERROR_IN_WRITING_FILE;
            }

            switch(img.getPixelFormat()) {
                case GL_LUMINANCE:
                case GL_ALPHA:
                    photometric = PHOTOMETRIC_MINISBLACK;
                    samplesPerPixel = 1;
                    break;
                case GL_LUMINANCE_ALPHA:
                    photometric = PHOTOMETRIC_MINISBLACK;
                    samplesPerPixel = 2;
                    break;
                case GL_RGB:
                    photometric = PHOTOMETRIC_RGB;
                    samplesPerPixel = 3;
                    break;
                case GL_RGBA:
                    photometric = PHOTOMETRIC_RGB;
                    samplesPerPixel = 4;
                    break;
                default:
                    return WriteResult::ERROR_IN_WRITING_FILE;
                    break;
            }

            switch(img.getDataType()){
                case GL_FLOAT:
                    TIFFSetField(image, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
                    TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, 1);
                    bitsPerSample = 32;
                    break;
                default:
                    bitsPerSample = 8;
                    break;
            }

            TIFFSetField(image, TIFFTAG_IMAGEWIDTH,img.s());
            TIFFSetField(image, TIFFTAG_IMAGELENGTH,img.t());
            TIFFSetField(image, TIFFTAG_BITSPERSAMPLE,bitsPerSample);
            TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL,samplesPerPixel);
            TIFFSetField(image, TIFFTAG_PHOTOMETRIC, photometric);
            TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS); 
            TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
            TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

            //uint32 rowsperstrip = TIFFDefaultStripSize(image, -1); 
            //TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
            
            // Write the information to the file
            for(int i = 0; i < img.t(); ++i) {
                TIFFWriteScanline(image,(tdata_t)img.data(0,img.t()-i-1),i,0);
            }

            // Close the file
            TIFFClose(image);

            return WriteResult::FILE_SAVED;
        }
开发者ID:joevandyk,项目名称:osg,代码行数:80,代码来源:ReaderWriterTIFF.cpp

示例14: process

// Main interface with NVTT
void NVTTProcessor::process( osg::Image& image, nvtt::Format format, bool generateMipMap, bool resizeToPowerOfTwo, CompressionMethod method, CompressionQuality quality)
{
    // Fill input options
    nvtt::InputOptions inputOptions;
    inputOptions.setTextureLayout(nvtt::TextureType_2D, image.s(), image.t() );
    inputOptions.setNormalMap(false);
    inputOptions.setConvertToNormalMap(false);
    inputOptions.setGamma(2.2f, 2.2f);
    inputOptions.setNormalizeMipmaps(false);
    inputOptions.setWrapMode(nvtt::WrapMode_Clamp);
    if (resizeToPowerOfTwo)
    {
        inputOptions.setRoundMode(nvtt::RoundMode_ToNearestPowerOfTwo);
    }
    inputOptions.setMipmapGeneration(generateMipMap);

    if (image.getPixelFormat() == GL_RGBA)
    {
        inputOptions.setAlphaMode( nvtt::AlphaMode_Transparency );
    }
    else
    {
        inputOptions.setAlphaMode( nvtt::AlphaMode_None );
    }
    std::vector<unsigned char> imageData( image.s() * image.t() * 4 );
    if (image.getPixelFormat() == GL_RGB)
    {
        convertRGBToBGRA( imageData, image );
    }
    else
    {
        convertRGBAToBGRA( imageData, image );
    }
    inputOptions.setMipmapData(&imageData[0],image.s(),image.t());

    // Fill compression options
    nvtt::CompressionOptions compressionOptions;
    switch(quality)
    {
      case FASTEST:
        compressionOptions.setQuality( nvtt::Quality_Fastest );
        break;
      case NORMAL:
        compressionOptions.setQuality( nvtt::Quality_Normal );
        break;
      case PRODUCTION:
        compressionOptions.setQuality( nvtt::Quality_Production);
        break;
      case HIGHEST:
        compressionOptions.setQuality( nvtt::Quality_Highest);
        break;
    }
    compressionOptions.setFormat( format );
    //compressionOptions.setQuantization(false,false,false);
    if (format == nvtt::Format_RGBA)
    {
        if (image.getPixelFormat() == GL_RGB)
        {
            compressionOptions.setPixelFormat(24,0xff,0xff00,0xff0000,0);
        }
        else
        {
            compressionOptions.setPixelFormat(32,0xff,0xff00,0xff0000,0xff000000);
        }
    }

    // Handler
    OSGImageOutputHandler outputHandler(format,image.getPixelFormat() == GL_RGB);
    VPBErrorHandler errorHandler;

    // Fill output options
    nvtt::OutputOptions outputOptions;
    outputOptions.setOutputHandler(&outputHandler);
    outputOptions.setErrorHandler(&errorHandler);
    outputOptions.setOutputHeader(false);

    // Process the compression now
    nvtt::Compressor compressor;
    if(method == USE_GPU)
    {
        compressor.enableCudaAcceleration(true);
        if(!compressor.isCudaAccelerationEnabled())
        {
            OSG_WARN<< "CUDA acceleration was enabled but it is not available. CPU will be used."<<std::endl;
        }
    }
    else
    {
        compressor.enableCudaAcceleration(false);
    }

    compressor.process(inputOptions,compressionOptions,outputOptions);

    outputHandler.assignImage(image);
}
开发者ID:Kurdakov,项目名称:emscripten_OSG,代码行数:96,代码来源:NVTTImageProcessor.cpp

示例15: writeImage

        virtual WriteResult writeImage(const osg::Image &img,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
        {
            std::string ext = osgDB::getFileExtension(fileName);
            if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;

            if (!img.isDataContiguous())
            {
                OSG_WARN<<"Warning: Writing of image data, that is non contiguous, is not supported by JPEG2000 plugin."<<std::endl;
                return WriteResult::ERROR_IN_WRITING_FILE;
            }

            jas_image_cmptparm_t cmptparms[4];
            jas_image_cmptparm_t *cmptparm;

            int internalFormat = osg::Image::computeNumComponents(img.getPixelFormat());

            jas_stream_t* mem = jas_stream_memopen((char*)img.data(), internalFormat*img.s()*img.t());

            /* Create an image of the correct size. */
            jas_image_t* jimage;
            int i;
            for (i = 0, cmptparm = cmptparms; i < internalFormat; ++i, ++cmptparm) {
                cmptparm->tlx = 0;
                cmptparm->tly = 0;
                cmptparm->hstep = 1;
                cmptparm->vstep = 1;
                cmptparm->width = img.s();
                cmptparm->height = img.t();
                cmptparm->prec = 8;
                cmptparm->sgnd = 0;
            }
            if (!(jimage = jas_image_create(internalFormat, cmptparms, JAS_CLRSPC_UNKNOWN))) {
                return WriteResult::ERROR_IN_WRITING_FILE;
            }

            if(internalFormat == 1)
            {
                jas_image_setclrspc(jimage, JAS_CLRSPC_GENGRAY);
                jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
            }
            else if(internalFormat == 2)
            {
                jas_image_setclrspc(jimage, JAS_CLRSPC_GENGRAY);
                jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
                jas_image_setcmpttype(jimage, 1, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_OPACITY));
            }
            else if(internalFormat == 3)
            {
                jas_image_setclrspc(jimage, JAS_CLRSPC_SRGB);
                jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
                jas_image_setcmpttype(jimage, 1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
                jas_image_setcmpttype(jimage, 2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
            }
            else if(internalFormat == 4)
            {
                jas_image_setclrspc(jimage, JAS_CLRSPC_SRGB);
                jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
                jas_image_setcmpttype(jimage, 1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
                jas_image_setcmpttype(jimage, 2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
                jas_image_setcmpttype(jimage, 3, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_OPACITY));
            }

            getdata(mem, jimage);

            FILE * fileHandle = osgDB::fopen(fileName.c_str(), "wb");
            if (!fileHandle) {
                return WriteResult::ERROR_IN_WRITING_FILE;
            }
            jas_stream_t* out = jas_stream_freopen(fileName.c_str(), "wb", fileHandle);        // Replacement for jas_stream_fopen() to be able to support UTF8
            if (!out) {
                fclose(fileHandle);
                return WriteResult::ERROR_IN_WRITING_FILE;
            }

            char* opt = 0;
            if(options)
            {
                opt = new char[options->getOptionString().size() + 1];
                strcpy(opt, options->getOptionString().c_str());
            }
            jas_image_encode(jimage, out, _fmt_jp2,  opt);
            if(opt) delete[] opt;

            jas_stream_flush(out);

            jas_stream_close(out);
            jas_image_destroy(jimage);
            fclose(fileHandle);

            return WriteResult::FILE_SAVED;
        }
开发者ID:Kurdakov,项目名称:emscripten_OSG,代码行数:91,代码来源:ReaderWriterJP2.cpp


注:本文中的osg::Image类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。