void cgAddDeclaration(cgTypes type, TString &prog, EShLanguage l) { switch (type) { case CG_TYPE_RESULT: if (g.result) { prog += getQualifierCode(g.result, l); prog += getTypeCode(g.result); prog += " "; prog += g.result->name; prog += ";\n"; } break; case CG_TYPE_CONDITION: if (g.condition) { prog += getQualifierCode(g.condition, l); prog += getTypeCode(g.condition); prog += " "; prog += g.condition->name; prog += ";\n"; } break; case CG_TYPE_PARAMETER: if (g.parameter) { prog += getQualifierCode(g.parameter, l); prog += getTypeCode(g.parameter); prog += " "; prog += g.parameter->name; if (g.parameter->isArray) { char buf[100]; sprintf(buf, "[%i]", g.parameter->arraySize[0]); prog += buf; } prog += ";\n"; } break; case CG_TYPE_LOOP_ITERS: for (strList::iterator it = g.loopIters.begin(); it != g.loopIters.end(); it++) { prog += "int "; prog += *it; prog += ";\n"; } break; case CG_TYPE_ALL: cgAddDeclaration(CG_TYPE_RESULT, prog, l); cgAddDeclaration(CG_TYPE_CONDITION, prog, l); cgAddDeclaration(CG_TYPE_PARAMETER, prog, l); cgAddDeclaration(CG_TYPE_LOOP_ITERS, prog, l); break; } }
/** * Saves the given matrix to the given stream. The header is modified * with appropriate data derived from the matrix structure. */ bool saveMatrix(const std::string& filename, const matrix<T>& theChannel, header& theHeader, const eCompressionType compr, dataCodec* codec) { //open stream std::ofstream out; out.open(filename.c_str(),std::ios::out|std::ios::binary); //stream ok? if (!(out.good() && out.is_open())) { std::string str = "Could not open file " + filename + " for writing."; setStatusString(str.c_str()); out.close(); return false; } theHeader.encoding.contents=getTypeCode(T()); theHeader.encoding.compression=compr; int tmpsize=sizeof(T)*theChannel.rows()*theChannel.columns(); int encsize = codec->estimateEncodedSize(tmpsize); // create the temporary buffer dataCodec::buffer tmp(encsize); // encode the data codec->encode((const ubyte*)&theChannel.at(0,0),tmpsize,&tmp.at(0),encsize); theHeader.size = encsize; theHeader.rows = theChannel.rows(); theHeader.columns = theChannel.columns(); // write the header if (!theHeader.write(out)) { setStatusString("Could not write header."); out.close(); return false; } // write the data out.write((const char*)(&tmp.at(0)),theHeader.size); out.close(); return true; }
/** * Loads a matrix from the given stream. The data are written * into theChannel, the meta-data are written into theHeader * These methods ignore all the parameters in this objects. * The only reason they are not static is that they modify * the status string. */ bool loadMatrix(const std::string& filename, matrix<T>& theChannel, header& theHeader) { theChannel.clear(); //open stream std::ifstream in; in.open(filename.c_str(),std::ios::in|std::ios::binary); //open failed? if (!(in.good() && in.is_open())) { std::string str = "Could not open file " + filename; setStatusString(str.c_str()); return false; } // read header if (!theHeader.read(in)) { setStatusString("Wrong header. Is this an LTI file?"); return false; } //create the right codec for this file dataCodec* codec=0; switch (theHeader.encoding.compression) { case None: codec=new identityCodec(); break; #if HAVE_LIBZ case Flate: codec=new flateCodec(); break; #endif case RunLength: codec=new runLengthCodec(); break; default: delete codec; setStatusString("Unknown codec of libz not installed"); return false; } _lti_debug2("type: " << theHeader.type << std::endl); _lti_debug2("encoding.contents: " << theHeader.encoding.contents << std::endl); _lti_debug2("encoding.compression: " << theHeader.encoding.compression << std::endl); _lti_debug2("size: " << theHeader.size << std::endl); _lti_debug2("rows: " << theHeader.rows << std::endl); _lti_debug2("columns: " << theHeader.columns << std::endl); if (theHeader.encoding.compression == None && theHeader.size != (sizeof(T)*theHeader.rows*theHeader.columns)) { setStatusString("Inconsistent header. Wrong file size"); delete codec; return false; } if (theHeader.encoding.contents != getTypeCode(T())) { bool flag=true; std::string str="Inconsistent file type. "; str+=theChannel.getTypeName(); str+" expected but "; // should we really try to recover from type errors??? // the following compiles, but might not get you what you expect switch(theHeader.encoding.contents) { case 'b': { str+=" lti::matrix<ubyte> found."; channel8 tmp; ioLTIworker<channel8::value_type> worker; if (!(flag=worker.loadBody(in,tmp,theHeader,codec))) { setStatusString(worker.getStatusString()); } theChannel.castFrom(tmp); break; } case 'i': { str+=" lti::imatrix found."; imatrix tmp; ioLTIworker<imatrix::value_type> worker; if (!(flag=worker.loadBody(in,tmp,theHeader,codec))) { setStatusString(worker.getStatusString()); } theChannel.castFrom(tmp); break; } case 'f': { str+=" lti::channel found."; flag=false; // channel tmp; // flag=loadBody(in,tmp); // // this generates a warning and will probabily generate funny // // images or channels // theChannel.castFrom(tmp); break; } case 'c': { // no chance: Forcing an image to something else is task-dependent str+=" lti::image found."; flag=false; // image tmp; // flag=loadBody(in,tmp); // theChannel.castFrom(tmp); break; } default: str+=" unknown type found."; flag=false; } setStatusString(str.c_str()); in.close(); delete codec; return flag; } else { bool flag=loadBody(in,theChannel,theHeader,codec); in.close(); delete codec; return flag; } }