v8::Local<v8::Object> DOMTypedArray<WTFTypedArray, V8TypedArray>::wrap(v8::Isolate* isolate, v8::Local<v8::Object> creationContext) { // It's possible that no one except for the new wrapper owns this object at // this moment, so we have to prevent GC to collect this object until the // object gets associated with the wrapper. RefPtr<ThisType> protect(this); ASSERT(!DOMDataStore::containsWrapper(this, isolate)); const WrapperTypeInfo* wrapperTypeInfo = this->wrapperTypeInfo(); RefPtr<DOMArrayBufferBase> buffer = this->bufferBase(); v8::Local<v8::Value> v8Buffer = toV8(buffer.get(), creationContext, isolate); if (v8Buffer.IsEmpty()) return v8::Local<v8::Object>(); ASSERT(isShared() == v8Buffer->IsSharedArrayBuffer()); v8::Local<v8::Object> wrapper; if (isShared()) { wrapper = V8TypedArray::New(v8Buffer.As<v8::SharedArrayBuffer>(), byteOffset(), length()); } else { wrapper = V8TypedArray::New(v8Buffer.As<v8::ArrayBuffer>(), byteOffset(), length()); } return associateWithWrapper(isolate, wrapperTypeInfo, wrapper); }
JSArrayBufferView* DataView::wrap(ExecState* exec, JSGlobalObject* globalObject) { #if 0 return JSDataView::create( exec, globalObject->typedArrayStructure(TypeDataView), buffer(), byteOffset(), byteLength()); #else return 0; #endif }
static VOID ProcessAddress (ADDRINT name, UINT32 size, ADDRINT ea, UINT32 bitIndex) { // Work out the base address of the array being accessed if (baseAddr == 0) { baseAddr = ea - byteOffset(size,bitIndex); return; } tests++; // Check that the position agrees with what we think it should be. if (baseAddr+byteOffset(size,bitIndex) != ea) { errors++; out << (const char *)name << ": Bad EA "; printHex(ea); out << " Expected "; printHex (baseAddr+byteOffset(size,bitIndex)); out << " Bit offset " << bitIndex << " Access size " << size << endl; } }
PassRefPtr<DataView> JSDataView::typedImpl() { return DataView::create(buffer(), byteOffset(), length()); }
JSArrayBufferView* DataView::wrap(ExecState* exec, JSGlobalObject* globalObject) { return JSDataView::create( exec, globalObject->typedArrayStructure(TypeDataView), possiblySharedBuffer(), byteOffset(), byteLength()); }
// niftiManager::readVector():reads 1D nifti vector ValueType niftiManager::readVector(const std::string& vectorFilenameRef, std::vector<float>* vectorPointer) const { std::vector<float>& vector = *vectorPointer; std::string vectorFilename( vectorFilenameRef ); std::string vectorFilenameZipped( vectorFilenameRef ); std::string extension; std::string zipExtension( boost::filesystem::path(vectorFilename).extension().string() ); if ( zipExtension == ".gz" ) { vectorFilename.resize(vectorFilename.size()-3); // Variables for calling system commands std::string sysCommand( "gzip -dcf " + vectorFilenameZipped + " > " + vectorFilename ); int success(system(sysCommand.c_str())); extension = ( boost::filesystem::path(vectorFilename).extension().string() ); } else if ( zipExtension == getFileExtension( ETFull ) || zipExtension == getFileExtension( ETCompact ) ) { extension = zipExtension; } else { std::cerr << "File \"" << vectorFilenameZipped << "\" has no recognized extension (\"" << zipExtension << "\") stopping." << std::endl; return VTError; } ValueType vectorValueType; if( extension == getFileExtension( ETFull ) ) { nifti_image* niftiImage = NULL; boost::mutex& ioMutex(boost::detail::thread::singleton<boost::mutex>::instance()) ; ioMutex.lock(); { niftiImage = nifti_image_read(vectorFilename.c_str(), 1); } ioMutex.unlock(); if( !niftiImage ) { std::cerr<< "ERROR @ niftiManager::readVector(): there was an error calling nifti_image_read() on vector "<< vectorFilename <<std::endl; return VTError; } if( niftiImage->ndim != 1 ) { std::cerr << "ERROR @ niftiManager::readVector(): nifti file has more than 1 dimension (" << niftiImage->ndim <<"): "<< niftiImage->nx <<" "<< niftiImage->ny <<" "<< niftiImage->nz <<" "<< niftiImage->nt <<" "<< niftiImage->nu <<" "<< niftiImage->nv <<std::endl; return VTError; } const size_t repType( niftiImage->datatype ); const size_t repByteSize( niftiImage->nbyper ); const size_t dataSize( niftiImage->nvox ); if( repType == DT_FLOAT32 ) { vectorValueType = VTFloat32; } else if ( repType == DT_UINT8 ) { vectorValueType = VTUINT8; } else { std::cerr<< "ERROR @ niftiManager::readVector(): vector representation type not recognized (not FLOAT32 nor UNIT8)" << std::endl; return VTError; } const size_t dimx( niftiImage->nx ); if( dimx != dataSize ) { std::cerr<< "ERROR @ niftiManager::readVector(): vector dimension does not match data size" << std::endl; return VTError; } vector.clear(); vector.resize(dataSize,0); void* niftiData(niftiImage->data); for (int i=0 ; i<dataSize ; ++i) { size_t byteOffset( i * repByteSize ); if( byteOffset >= dataSize ) { std::cerr<< "ERROR @ niftiManager::readVector():: pointer offset was too high when loading nifti data" << std::endl; return VTError; } void* dataPos = static_cast<unsigned char*>(niftiData) + byteOffset; if (repType == DT_UINT8) { unsigned char datapoint = *(static_cast<unsigned char*>(dataPos)); vector[i] = (float)datapoint; } else if (repType == DT_FLOAT32) { float datapoint = *(static_cast<float*>(dataPos)); vector[i] = datapoint; } else { std::cerr<< "ERROR @ niftiManager::readVector(): vector representation type not recognized (not FLOAT32 nor UNIT8)" << std::endl; return VTError; } } // clean up nifti_image_free( niftiImage ); } else if ( extension == getFileExtension( ETCompact ) ) { std::ifstream inFile; inFile.open( vectorFilename.c_str(), std::ios::in | std::ios::binary | std::ios::ate ); if( !inFile.is_open() ) { std::stringstream errormessage; errormessage << "ERROR @ niftiManager::readVector(): there was an error opening the input image file "<< vectorFilename <<std::endl; throw std::runtime_error( errormessage.str() ); } size_t dataSize = inFile.tellg(); void* data = malloc( dataSize ); inFile.seekg( 0, std::ios::beg ); boost::mutex& ioMutex(boost::detail::thread::singleton<boost::mutex>::instance()) ; ioMutex.lock(); { inFile.read( static_cast<char*>(data), dataSize ); } ioMutex.unlock(); inFile.close(); const size_t headerSize = UINT32_SIZE + UINT32_SIZE; void* dataPos = static_cast<unsigned char*>(data); const size_t repType = *(static_cast<uint32_t*>(dataPos)); dataPos = static_cast<unsigned char*>(data) + UINT32_SIZE; const size_t dimx = *(static_cast<uint32_t*>(dataPos)); size_t repByteSize( 0 ); if( repType == (FLOAT_SIZE * CHAR_BIT) ) { vectorValueType = VTFloat32; repByteSize = FLOAT_SIZE; } else if ( repType == (UINT8_SIZE * CHAR_BIT) ) { vectorValueType = VTUINT8; repByteSize = UINT8_SIZE; } else { std::cerr<< "ERROR @ niftiManager::readVector(): vector representation type not recognized: " << repType << " (not FLOAT32 nor UNIT8)" << std::endl; return VTError; } const size_t dataSizeTest = headerSize + ( dimx * repByteSize ); if( dataSize != dataSizeTest ) { std::stringstream errormessage; errormessage << "ERROR @ niftiManager::readVector(): file data as read: "<< dataSize << " is different as file data according to header: " << dataSizeTest <<std::endl; errormessage<<" headerSize: "<< headerSize <<" dimx: "<< dimx <<" repSize: "<<repByteSize << std::endl; throw std::runtime_error( errormessage.str() ); } vector.clear(); vector.resize(dimx,0); for( size_t i = 0; i < dimx; ++i ) { size_t byteOffset( headerSize + ( i * repByteSize ) ); if( byteOffset >= dataSize ) { std::cerr<< "ERROR @ niftiManager::readVector():: pointer offset was too high when loading nifti data" << std::endl; return VTError; } dataPos = static_cast<unsigned char*>(data) + byteOffset; if ( vectorValueType == VTUINT8 ) { uint8_t datapoint = *(static_cast<uint8_t*>(dataPos)); vector[i] = (float)datapoint; } else if ( vectorValueType == VTFloat32 ) { float datapoint = *(static_cast<float*>(dataPos)); vector[i] = datapoint; } else { std::cerr<< "ERROR @ niftiManager::readVector(): vector representation type not recognized (not FLOAT32 nor UNIT8)" << std::endl; return VTError; } } // clean up free( data ); } else { std::cerr << "File \"" << vectorFilename << "\" has no recognized extension (\"" << extension << "\") stopping." << std::endl; return VTError; } if ( zipExtension == ".gz" ) { // Variables for calling system commands std::string sysCommand( "rm -f " + vectorFilename ); int success(system(sysCommand.c_str())); } return vectorValueType; }// end niftiManager::readVector() ----------------------------------------------------------------
// "writeImage()": writes a 3D image void niftiManager::writeImage( const std::string& imageFilename, const ValueType dataValueType, const std::vector<std::vector<std::vector<float> > >& image, bool doZip ) const { if (image.empty()) { std::cerr<< "ERROR @ niftiManager::writeImage(): image matrix is empty, image has not been written" <<std::endl; return; } const size_t dimx = image.size(); const size_t dimy = image[0].size(); const size_t dimz = image[0][0].size(); nifti_1_header imageHeader; imageHeader.sizeof_hdr = 0; if(m_header.sizeof_hdr != 0) { imageHeader = m_header; } generateHeader( dimx, dimy, dimz, dataValueType, &imageHeader); nifti_image* niftiImage = nifti_convert_nhdr2nim( imageHeader, NULL); const size_t repByteSize( niftiImage->nbyper ); const size_t dataSize( niftiImage->nvox ); const size_t repType( niftiImage->datatype ); if ( dataSize != (dimx*dimy*dimz) ) { throw std::runtime_error( "niftiManager::writeImage(): image header nvox and matrix size do not match" ); } if ( repType != imageHeader.datatype ) { throw std::runtime_error( "niftiManager::writeImage(): created header datatype and input argument dont match" ); } if(niftiImage->data != NULL) { throw std::runtime_error( "niftiManager::writeVector(): nifti_convert_nhdr2nim had already allocated memory" ); } niftiImage->data = malloc (dataSize * repByteSize); for (int i=0 ; i<dimz ; ++i) { for (int j=0 ; j<dimy ; ++j) { for (int k=0 ; k<dimx ; ++k) { size_t voxOffset( (i*dimy*dimx) + (j*dimx) + k ); if( voxOffset >= dataSize ) { throw std::runtime_error( "ERROR @ niftiManager::writeImage():: pointer offset was too high when loading nifti data"); } size_t byteOffset( voxOffset * repByteSize ); void* dataPos = static_cast<unsigned char*>(niftiImage->data) + byteOffset; if (repType == DT_UINT8) { unsigned char datapoint = (unsigned char)image[k][j][i]; *(static_cast<unsigned char*>(dataPos)) = datapoint; } else if (repType == DT_FLOAT32) { float datapoint = image[k][j][i]; *(static_cast<float*>(dataPos)) = datapoint; } else { throw std::runtime_error("ERROR @ niftiManager::writeImage(): image representation type not recognized (neither UINT8 nor FLOAT32)"); } } } } if( nifti_set_filenames(niftiImage, imageFilename.c_str(), 0, 1) ) { std::stringstream errormessage; errormessage << "ERROR @ niftiManager::writeImage(): there was an error calling nifti_set_filenames() on output image file "<< imageFilename <<std::endl; throw std::runtime_error( errormessage.str() ); } boost::mutex& ioMutex(boost::detail::thread::singleton<boost::mutex>::instance()) ; ioMutex.lock(); { nifti_image_write( niftiImage ); } ioMutex.unlock(); // clean up nifti_image_free( niftiImage ); if (doZip) { // zip file // Variables for calling system commands std::string gzipString("gzip -f "); std::string sysCommand(gzipString + imageFilename); int success(system(sysCommand.c_str())); } return; }// end niftiManager::writeImage() -----------------------------------------------------------------
// "readImage()": reads a 3D image ValueType niftiManager::readImage( const std::string& imageFilenameRef, std::vector<std::vector<std::vector<float> > >* imagePointer ) const { std::vector<std::vector<std::vector<float> > >& image( *imagePointer ); std::string imageFilename( imageFilenameRef ); std::string imageFilenameZipped( imageFilenameRef ); std::string extension( boost::filesystem::path(imageFilename).extension().string() ); if ( extension == ".gz" ) { imageFilename.resize(imageFilename.size()-3); // Variables for calling system commands std::string sysCommand( "gzip -dcf " + imageFilenameZipped + " > " + imageFilename ); int success(system(sysCommand.c_str())); } else if ( extension == getFileExtension( ETFull ) ) { } else { std::cerr << "File \"" << imageFilenameZipped << "\" has no recognized extension (\"" << extension << "\") stopping." << std::endl; return VTError; } nifti_image* niftiImage = NULL; boost::mutex& ioMutex(boost::detail::thread::singleton<boost::mutex>::instance()) ; ioMutex.lock(); { niftiImage = nifti_image_read(imageFilename.c_str(), 1); } ioMutex.unlock(); if( !niftiImage ) { std::cerr<< "ERROR @ niftiManager::readImage(): there was an error calling nifti_image_read() on image file "<< imageFilename <<std::endl; return VTError; } if( niftiImage->ndim > 3 ) { std::cerr << "ERROR @ niftiManager::readImage(): nifti file has more than 3 dimensions (" << niftiImage->ndim <<"): "<< niftiImage->nx <<" "<< niftiImage->ny <<" "<< niftiImage->nz <<" "<< niftiImage->nt <<" "<< niftiImage->nu <<" "<< niftiImage->nv <<std::endl; return VTError; } const size_t repType( niftiImage->datatype ); const size_t repByteSize( niftiImage->nbyper ); const size_t dataSize( niftiImage->nvox ); ValueType imageValueType; if(repType == DT_UINT8 ) { imageValueType = VTUINT8; } else if(repType == DT_FLOAT32 ) { imageValueType = VTFloat32; } else { std::cerr<< "ERROR @ niftiManager::readImage(): image representation type not recognized (neither UINT8 nor FLOAT32)" << std::endl; return VTError; } const size_t dimx( niftiImage->nx ); const size_t dimy( niftiImage->ny ); const size_t dimz( niftiImage->nz); image.clear(); { std::vector<float> zvector(dimz,0); std::vector<std::vector<float> >yzmatrix(dimy,zvector); image.resize(dimx,yzmatrix); } void* niftiData(niftiImage->data); for (int i=0 ; i<dimz ; ++i) { for (int j=0 ; j<dimy ; ++j) { for (int k=0 ; k<dimx ; ++k) { size_t voxOffset( (i*dimy*dimx) + (j*dimx) + k ); if( voxOffset >= dataSize ) { std::cerr<< "ERROR @ niftiManager::readImage():: pointer offset was too high when loading nifti data" << std::endl; return VTError; } size_t byteOffset( voxOffset * repByteSize ); void* dataPos = static_cast<unsigned char*>(niftiData) + byteOffset; if (repType == DT_UINT8) { unsigned char datapoint = *(static_cast<unsigned char*>(dataPos)); image[k][j][i] = (float)datapoint; } else if (repType == DT_FLOAT32) { float datapoint = *(static_cast<float*>(dataPos)); image[k][j][i] = datapoint; } else { std::cerr<< "ERROR @ niftiManager::readImage(): image representation type not recognized (neither UINT8 nor FLOAT32)" << std::endl; return VTError; } } } } // clean up nifti_image_free( niftiImage ); if ( extension == ".gz" ) { // Variables for calling system commands std::string sysCommand( "rm -f " + imageFilename ); int success(system(sysCommand.c_str())); } return imageValueType; }// end niftiManager::readImage() -----------------------------------------------------------------