⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtimportexport.cpp

📁 最新osg包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* *  QTImportExport.cpp *  cefix * *  Created by Stephan Huber on 07.02.08. *  Copyright 2008 __MyCompanyName__. All rights reserved. * */#include <map>#include <sstream>#include "QTImportExport.h"#include "QTUtils.h"#include <osgDB/FileNameUtils>/** small exception class bundling a error-message */class QTImportExportException : public std::exception {    public:        QTImportExportException(int err, const std::string& msg) : std::exception(), _err(err), _msg(msg) {}                virtual const char* what() { return _msg.c_str(); }        int getErrorCode() { return _err; }                virtual ~QTImportExportException() throw () {}            private:        int _err;        std::string _msg;};QuicktimeImportExport::QuicktimeImportExport() :    _error(0),     _lastError(""){    initQuicktime();}// ----------------------------------------------------------------------------------------------------------// flipImage// ----------------------------------------------------------------------------------------------------------void QuicktimeImportExport::flipImage(unsigned char* pixels, int bytesPerPixel, unsigned int width, unsigned height) {    // Flip the image    unsigned imageSize = width * height * bytesPerPixel;    char *tBuffer = new char [imageSize];    unsigned int rowBytes = width * bytesPerPixel;    unsigned int i,j;    for (i = 0, j = imageSize - rowBytes; i < imageSize; i += rowBytes, j -= rowBytes)        memcpy( &tBuffer[j], &pixels[i], (size_t)rowBytes );    memcpy(pixels, tBuffer, (size_t)imageSize);    delete[] tBuffer;}// ----------------------------------------------------------------------------------------------------------// prepareBufferForOSG// ----------------------------------------------------------------------------------------------------------unsigned char* QuicktimeImportExport::pepareBufferForOSG(unsigned char * buffer, int bytesPerPixel, unsigned int width, unsigned int height){    unsigned char *pixels = new unsigned char [height * width * 4];    unsigned char *dstp = pixels;    unsigned char *srcp = buffer;    unsigned int i, j;        int roffset, goffset, boffset, aoffset;    aoffset = -1;    int sourceStep;        switch (bytesPerPixel) {        case 1:            sourceStep = 1;            roffset = goffset = boffset = 0;            break;        case 3:            sourceStep = 3;            roffset = 0;            goffset = 1;            boffset = 2;            break;        case 4:            sourceStep = 4;            aoffset = 1;            roffset = 2;            goffset = 3;            boffset = 0;            break;                }                        for (i = 0; i < height; ++i )     {        for (j = 0; j < width; ++j )         {            dstp[0] = (aoffset < 0) ? 0 : srcp[aoffset];            dstp[1] = srcp[roffset];            dstp[2] = srcp[goffset];            dstp[3] = srcp[boffset];            srcp+=sourceStep;            dstp+=4;                    }    }        flipImage(pixels, bytesPerPixel, width, height);    return pixels;}// ----------------------------------------------------------------------------------------------------------// prepareBufferForQuicktime// ----------------------------------------------------------------------------------------------------------unsigned char* QuicktimeImportExport::prepareBufferForQuicktime(unsigned char* buffer, GLenum pixelFormat, int bytesPerPixel, unsigned int width, unsigned int height) {    unsigned char *pixels = new unsigned char [height * width * 4];    unsigned char *dstp = pixels;    unsigned char *srcp = buffer;    unsigned int i, j;        int roffset, goffset, boffset, aoffset;    aoffset = -1;    int sourceStep;        switch (bytesPerPixel) {        case 1:            sourceStep = 1;            roffset = goffset = boffset = 0;            break;        case 3:            sourceStep = 3;            roffset = 0;            goffset = 1;            boffset = 2;            break;        case 4:            sourceStep = 4;            switch (pixelFormat) {                case GL_RGBA:                    aoffset = 3;                    roffset = 0;                    goffset = 1;                    boffset = 2;                    break;                                case GL_BGRA_EXT:                    aoffset = 0;                    roffset = 1;                    goffset = 2;                    boffset = 3;                    break;            }    }                                    for (i = 0; i < height; ++i )     {        for (j = 0; j < width; ++j )         {            dstp[0] = (aoffset < 0) ? 0 : srcp[aoffset];            dstp[1] = srcp[roffset];            dstp[2] = srcp[goffset];            dstp[3] = srcp[boffset];            srcp+=sourceStep;            dstp+=4;                    }    }        flipImage(pixels, 4, width, height);        return pixels;}// ----------------------------------------------------------------------------------------------------------// readFromStream// ----------------------------------------------------------------------------------------------------------osg::Image* QuicktimeImportExport::readFromStream(std::istream & inStream, const std::string& fileTypeHint, long sizeHint) {    char* content = NULL;    long length = 0;     if (sizeHint != 0)     {        length = sizeHint;        content = new char[length];        inStream.read (content,length);    }    else     {        int readBytes(0), newBytes(0);                char buffer[10240];                while (!inStream.eof()) {            inStream.read(buffer, 10240);            newBytes = inStream.gcount();            if (newBytes > 0) {                char* newcontent = new char[readBytes + newBytes];                            if (readBytes > 0)                    memcpy(newcontent, content, readBytes);                                memcpy(&newcontent[readBytes], &buffer, newBytes);                readBytes += newBytes;                if (content) delete[] content;                content = newcontent;            }        }        length = readBytes;    }        osg::Image* img = doImport(reinterpret_cast<unsigned char*>(content), length, fileTypeHint);        if (content) delete[] content;    return img; }  Handle getPtrDataRef(unsigned char *data, unsigned int size, const std::string &filename){     // Load Data Reference     Handle dataRef;     Handle fileNameHandle;     PointerDataRefRecord ptrDataRefRec;     ComponentInstance dataRefHandler;     unsigned char pstr[255];      ptrDataRefRec.data = data;     ptrDataRefRec.dataLength = size;      /*err = */PtrToHand(&ptrDataRefRec, &dataRef, sizeof(PointerDataRefRecord));      // Open a Data Handler for the Data Reference     /*err = */OpenADataHandler(dataRef, PointerDataHandlerSubType, NULL,         (OSType)0, NULL, kDataHCanRead, &dataRefHandler);      // Convert From CString in filename to a PascalString in pstr     if (filename.length() > 255) {         //hmm...not good, pascal string limit is 255!         //do some error handling maybe?!         throw QTImportExportException(0, "filename length limit exceeded");     }     CopyCStringToPascal(filename.c_str(), pstr);     // Add filename extension     /*err = */PtrToHand(pstr, &fileNameHandle, filename.length() + 1);     /*err = */DataHSetDataRefExtension(dataRefHandler, fileNameHandle,         kDataRefExtensionFileName);     DisposeHandle(fileNameHandle);      // Release old handler which does not have the extensions     DisposeHandle(dataRef); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -