예제 #1
0
//
// Read image data
//
bool CCCDFile::GetImageData(CImage &img, CmpackBitpix bitpix, GError **error)
{
	assert (m_Handle != NULL);

	CmpackImage *i;
	int res = cmpack_ccd_to_image(m_Handle, bitpix, &i);
	if (res==0) 
		img.Assign(i);
	else {
		char *msg = cmpack_formaterror(res);
		g_set_error(error, g_AppError, res, msg);
		cmpack_free(msg);
	}
	return (res==0);
}
예제 #2
0
bool
CIMGCodec::Decode( CORE::CIOAccess& encodedInput ,
                   CImage& outputImage           )
{GUCEF_TRACE;

    CORE::CDynamicBuffer outputBuffer( 102400, true );
    CORE::CDynamicBufferAccess bufferAccess( &outputBuffer, false );
    
    if ( Decode( encodedInput  ,
                 bufferAccess  ) )
    {
        bufferAccess.Setpos( 0 );
        UInt32 bytesRead = 0;
        bufferAccess.Setpos( 0 );
        
        // Fill our header from the buffer
        TImageInfo imageInfo;
        bytesRead = bufferAccess.Read( &imageInfo, sizeof( TImageInfo ), 1 );
        if ( bytesRead !=  sizeof( TImageInfo ) ) return false;
        
        // Check the header version for compatibility
        if ( imageInfo.version != GUCEF_IMAGE_TIMAGEINFO_VERSION ) return false;
        
        // Check if we are finished already
        if ( imageInfo.nrOfFramesInImage == 0 ) return true;
        
        // Process the image frames
        TImageFrame* imageFrameInfo = new TImageFrame[ imageInfo.nrOfFramesInImage ]; 
        for ( UInt32 i=0; i<imageInfo.nrOfFramesInImage; ++i )
        {            
            // Read the frame info
            TImageFrameInfo* currentFrame = &imageFrameInfo[ i ].frameInfo;
            bytesRead = bufferAccess.Read( currentFrame, sizeof( TImageFrameInfo ), 1 );
            
            // Sanity check 
            if ( ( bytesRead != sizeof( TImageFrameInfo ) )                       ||
                 ( currentFrame->version != GUCEF_IMAGE_TIMAGEFRAMEINFO_VERSION )  )
            {
                delete []imageFrameInfo;
                return false;
            }
            
            imageFrameInfo[ i ].mipmapLevel = new TImageMipMapLevel[ currentFrame->nrOfMipmapLevels ];
            for ( UInt32 n=0; n<currentFrame->nrOfMipmapLevels; ++n )
            {
                // Read the frame's mipmap info
                TImageMipMapLevelInfo* mipMapLevel = &imageFrameInfo[ i ].mipmapLevel[ n ].mipLevelInfo;
                bytesRead = bufferAccess.Read( mipMapLevel, sizeof( TImageMipMapLevelInfo ), 1 );
                
                // Sanity check
                if ( ( bytesRead != sizeof( TImageMipMapLevelInfo ) )                      ||
                     ( mipMapLevel->version != GUCEF_IMAGE_TIMAGEMIPMAPLEVELINFO_VERSION )  )
                {
                    for ( UInt32 m=0; m<n; ++m ) delete []imageFrameInfo[ m ].mipmapLevel;
                    delete []imageFrameInfo;
                    return false;
                }
            }
        }

        // Now we can read all the pixel data
        CImage::TFrameList frameList;
        CImage::TMipMapList mipMapList;
        UInt32 bufferOffset = bufferAccess.Tell();        
        char* pixelBuffer = static_cast< char* >( outputBuffer.GetBufferPtr() ) + bufferOffset;
        for ( UInt32 i=0; i<imageInfo.nrOfFramesInImage; ++i )
        {
            TImageFrameInfo* currentFrame = &imageFrameInfo[ i ].frameInfo;
            for ( UInt32 n=0; n<currentFrame->nrOfMipmapLevels; ++n )
            {
                // Create a pixelmap using the information we obtained + out pixelBuffer pointer
                TImageMipMapLevelInfo* mipMapLevel = &imageFrameInfo[ i ].mipmapLevel[ n ].mipLevelInfo;
                
                // Obtain the expected pixel map size in bytes
                TPixelStorageFormat pixelStorageFormat = (TPixelStorageFormat) mipMapLevel->pixelStorageFormat;
                TBuildinDataType pixelComponentDataType = (TBuildinDataType) mipMapLevel->pixelComponentDataType;
                UInt32 pixelMapSize = CPixelMap::GetExpectedPixelMapSize( mipMapLevel->frameWidth  ,
                                                                          mipMapLevel->frameHeight ,
                                                                          pixelStorageFormat       ,
                                                                          pixelComponentDataType   );
                
                // Check to make sure the buffer is large enough for the data we expect
                if ( pixelMapSize <= outputBuffer.GetDataSize() - bufferOffset )
                {
                    CPixelMap* pixelMap = new CPixelMap( pixelBuffer              ,
                                                         mipMapLevel->frameWidth  ,
                                                         mipMapLevel->frameHeight ,
                                                         pixelStorageFormat       ,
                                                         pixelComponentDataType   );                    
                    
                    // Add this mipmap level to the frame
                    mipMapList.push_back( TPixelMapPtr( pixelMap ) );
                    
                    if ( i+1 < imageInfo.nrOfFramesInImage )
                    {
                        // Jump to the next image component in the buffer
                        UInt32 pixelBlockSize = ( mipMapLevel->frameWidth * mipMapLevel->frameHeight ) * pixelMap->GetSizeOfPixelInBytes();
                        bufferOffset += pixelBlockSize;
                        if ( bufferOffset < outputBuffer.GetDataSize() )
                        {
                            pixelBuffer += pixelBlockSize;
                        }
                        else
                        {
                            // oh oh,.. we ran out of pixels in our buffer even though our image info
                            // suggests we should have more pixels
                            for ( UInt32 m=n; m<currentFrame->nrOfMipmapLevels; ++m ) delete []imageFrameInfo[ m ].mipmapLevel;
                            delete []imageFrameInfo;
                            return false;
                        }
                    }
                }
                else
                {
                    // oh oh,.. we ran out of pixels in our buffer even though our image info
                    // suggests we should have more pixels
                    for ( UInt32 m=n; m<currentFrame->nrOfMipmapLevels; ++m ) delete []imageFrameInfo[ m ].mipmapLevel;
                    delete []imageFrameInfo;
                    return false;
                }
            }
            
            // Add the frame with all it's mipmap levels to the frame list
            frameList.push_back( mipMapList );
            mipMapList.clear();
            
            // Clean up our info storage
            delete []imageFrameInfo[ i ].mipmapLevel;
        }
                
        // Clean up our info storage
        delete []imageFrameInfo;
        
        // Assign the parsed data to the output image object
        outputImage.Assign( frameList );
        return true;
    }
    
    return false;
}