Exemplo n.º 1
0
int main(int argc, char **argv) 
{
  setOptions(argc, argv);
  showOptions();

  SpecificationFile sFile(modelFile);
  SUT *sut = new SUT(sFile.parameter, sFile.value, sFile.tway);

  if (constraintFile != "") {
    ConstraintFile cFile(constraintFile);
    sut->setConstraint(cFile.getClauses());
  }

  Framework* generator = initGenerator(algorithm, sut);

  srand((unsigned int)time(0));
  OutputFile outFile;
  for( int i = 0 ; i < repeat ; i++ ) {
    generator->PSOEvolve();
    outFile.update(generator->ARRAY, sut->parameter, generator->TIME);
  }
  outFile.write(sut, outputFile);
  cout << "Best Size = " << outFile.size << " Time = " << outFile.time << endl;

	return 0;
}
Exemplo n.º 2
0
static int imb_save_openexr_float(struct ImBuf *ibuf, const char *name, int flags)
{
	int channels = ibuf->channels;
	int width = ibuf->x;
	int height = ibuf->y;
	int write_zbuf = (flags & IB_zbuffloat) && ibuf->zbuf_float != NULL;   // summarize

	try
	{
		Header header (width, height);
		
		openexr_header_compression(&header, ibuf->ftype & OPENEXR_COMPRESS);
		openexr_header_metadata(&header, ibuf);
		
		header.channels().insert ("R", Channel (FLOAT));
		header.channels().insert ("G", Channel (FLOAT));
		header.channels().insert ("B", Channel (FLOAT));
		if (ibuf->depth==32 && channels >= 4)
			header.channels().insert ("A", Channel (FLOAT));
		if (write_zbuf)
			header.channels().insert ("Z", Channel (FLOAT));
		
		FrameBuffer frameBuffer;			
		OutputFile *file = new OutputFile(name, header);			
		int xstride = sizeof(float) * channels;
		int ystride = - xstride*width;
		float *rect[4] = {NULL, NULL, NULL, NULL};

		/* last scanline, stride negative */
		rect[0]= ibuf->rect_float + channels*(height-1)*width;
		rect[1]= rect[0]+1;
		rect[2]= rect[0]+2;
		rect[3]= (channels >= 4)? rect[0]+3:rect[0]; /* red as alpha, is this needed since alpha isnt written? */

		frameBuffer.insert ("R", Slice (FLOAT,  (char *)rect[0], xstride, ystride));
		frameBuffer.insert ("G", Slice (FLOAT,  (char *)rect[1], xstride, ystride));
		frameBuffer.insert ("B", Slice (FLOAT,  (char *)rect[2], xstride, ystride));
		if (ibuf->depth==32 && channels >= 4)
			frameBuffer.insert ("A", Slice (FLOAT,  (char *)rect[3], xstride, ystride));
		if (write_zbuf)
			frameBuffer.insert ("Z", Slice (FLOAT, (char *) (ibuf->zbuf_float + (height-1)*width),
											sizeof(float), sizeof(float) * -width));
		file->setFrameBuffer (frameBuffer);				  
		file->writePixels (height);					  
		delete file;
	}
	catch (const std::exception &exc)
	{      
		printf("OpenEXR-save: ERROR: %s\n", exc.what());
		if (ibuf) IMB_freeImBuf(ibuf);
		
		return (0);
	}
	
	return (1);
	//	printf("OpenEXR-save: Done.\n");
}
Exemplo n.º 3
0
bool HashSet::store( ) const
{
   OutputFile out;

   for ( const auto &id : _set )
      id.store( out );

   return out.rename( path_get( PathType::OBJ, "index" ).string( ) );
}
Exemplo n.º 4
0
	void write_to_exr_file (const string& file_name_) {
		Header header (m_x_res, m_y_res);
		
		//edit the active zone.
		Box2i data_window (V2i (0, 0),
						   V2i (m_x_res - 1, m_y_res - 1));

		header.dataWindow() = data_window; //beuark.

		header.channels().insert ("R", Channel (HALF));
		header.channels().insert ("G", Channel (HALF));
		header.channels().insert ("B", Channel (HALF));	
	
		const int x_count = m_x_res;
		const int nb_pixels = m_x_res * m_y_res;
	
		half * half_rgb = new half[3 * nb_pixels];	
	
		int offset = 0;
		int num_pixel = 0;
	
		for (int y = 0; y < m_y_res; y++) {
			for (int x = 0; x < m_x_res; x++, num_pixel++) {
				Color color = pixel (x, y);
			
				for (int i = 0; i < 3; i++, offset++) {
					half_rgb[offset] = color[i];
				}			
			}
		}
	
		offset = 0;
	
		half_rgb -= 3 * offset;	

		FrameBuffer fb;
		//there are 3 * sizeof(half) bytes between two R elements.
		fb.insert ("R", Slice (HALF, (char *)half_rgb, 3 * sizeof (half),
							   3 * x_count * sizeof (half)));
		//the first element of G is sizeof(half) after the first element of R.
		fb.insert ("G", Slice (HALF, (char *)half_rgb + sizeof(half), 3 * sizeof (half),
							   3 * x_count * sizeof (half)));
		//the first B element is 2 * sizeof (half) bytes after the first element of R.
		fb.insert ("B", Slice (HALF, (char *)half_rgb + 2 * sizeof(half), 3 * sizeof (half),
							   3 * x_count * sizeof (half)));
		try {
			OutputFile file (file_name_.c_str(), header);
			file.setFrameBuffer (fb);
			//y_count() rows to write
			file.writePixels (m_y_res);
		} catch (const std::exception &e) {
			std::cerr<<"Unable to write image file "<<file_name_<<" : "<<e.what()<<std::endl;
		}

		//release the memory, but come back to the real address before.
		delete[] (half_rgb + 3 * offset);	
	}
Exemplo n.º 5
0
void saveEXRRGBA(const char* filename, int width, int height, float* data)
{
	half *idr_r = new half[ width * height];
	half *idr_g = new half[ width * height];
	half *idr_b = new half[ width * height];
	half *idr_a = new half[ width * height];
	
	for(int j=0; j< height; j++) {
		int invj = height - 1 -j;
		for(int i=0; i< width; i++) {
			idr_r[j* width + i] = (half)data[(invj* width + i)*4];
			idr_g[j* width + i] = (half)data[(invj* width + i)*4+1];
			idr_b[j* width + i] = (half)data[(invj* width + i)*4+2];
			idr_a[j* width + i] = (half)data[(invj* width + i)*4+3];
		}
	}
// write exr
	Header idrheader ( width,  height); 

		idrheader.channels().insert ("R", Channel (HALF));
		idrheader.channels().insert ("G", Channel (HALF));                                   // 1 
        idrheader.channels().insert ("B", Channel (HALF));
		idrheader.channels().insert ("A", Channel (HALF));                   // 2  
    
        OutputFile idrfile (filename, idrheader);                               // 4 
        FrameBuffer idrframeBuffer;
		 idrframeBuffer.insert ("R",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) idr_r,            // base   // 8 
                                   sizeof (*idr_r) * 1,       // xStride// 9 
                                   sizeof (*idr_r) *  width));
        idrframeBuffer.insert ("G",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) idr_g,            // base   // 8 
                                   sizeof (*idr_g) * 1,       // xStride// 9 
                                   sizeof (*idr_g) *  width));
		 idrframeBuffer.insert ("B",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) idr_b,            // base   // 8 
                                   sizeof (*idr_b) * 1,       // xStride// 9 
                                   sizeof (*idr_b) *  width));
		 idrframeBuffer.insert ("A",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) idr_a,            // base   // 8 
                                   sizeof (*idr_a) * 1,       // xStride// 9 
                                   sizeof (*idr_a) *  width));
       
        idrfile.setFrameBuffer (idrframeBuffer);                                // 16 
        idrfile.writePixels ( height); 
        
// cleanup
	delete[] idr_r;
	delete[] idr_g;
	delete[] idr_b;
	delete[] idr_a;
}
EStatusCode PreprocessorTest::RunTest(const string& inName, const string& inOriginalFile, const string& inOutputFile, const string& inComparisonFile)
{
	EStatusCode status = eSuccess;

	StringToStringMap preprocessorDefinitions;
	StringList includeFolders;

	includeFolders.push_back(scSamplesBasePath);
	preprocessorDefinitions.insert(StringToStringMap::value_type("PREDEFINED_SYMBOL","2"));

	InputFile sourceFile;
	sourceFile.OpenFile(inOriginalFile);

	OutputFile outputFile;
	outputFile.OpenFile(inOutputFile);
	mCurrentStream = outputFile.GetOutputStream();

	PreProcessor preProcessor;

	preProcessor.Setup(sourceFile.GetInputStream(),inOriginalFile,preprocessorDefinitions,includeFolders);

	preProcessor.AddListener(this);

	mStartRow = true;
	BoolAndString tokenizerResult = preProcessor.GetNextToken();
	while(tokenizerResult.first)
	{
		if(!mStartRow)
			mCurrentStream->Write((const Byte*)"  ",2); // 2 spaces, so we can clearly distinct tokens
		mCurrentStream->Write((const Byte*)tokenizerResult.second.c_str(),tokenizerResult.second.size());

		mStartRow = false;

		tokenizerResult = preProcessor.GetNextToken();
	}

	sourceFile.CloseFile();
	outputFile.CloseFile();

	mCurrentStream = NULL;
	
	SimpleFileComparer comparer;

	if(!comparer.Same(inOutputFile,inComparisonFile))
	{
		cout<<"TokenizerTest::Run, failed in test named "<<inName<<". see result in "<<inOutputFile<<" and compare with the required result in "<<inComparisonFile<<"\n";
		status = eFailure;
	}

	return status;	

}
OutputFile *OutputFile::CreateTemporary(const std::string &pFileTemplate,
                                        unsigned pFlags) {
    char *tmp_filename = NULL;
    int tmp_fd;
    OutputFile *result = NULL;

    // Allocate memory to hold the generated unique temporary filename.
    tmp_filename =
        new (std::nothrow) char [ pFileTemplate.length() + /* .XXXXXX */7 + 1 ];
    if (tmp_filename == NULL) {
        ALOGE("Out of memory when allocates memory for filename %s in "
              "OutputFile::CreateTemporary()!", pFileTemplate.c_str());
        return NULL;
    }

    // Construct filename template for mkstemp().
    if (pFileTemplate.length() > 0)
        ::memcpy(tmp_filename, pFileTemplate.c_str(), pFileTemplate.length());
    ::strncpy(tmp_filename + pFileTemplate.length(), ".XXXXXX", 7);

    // POSIX mkstemp() never returns EINTR.
    tmp_fd = ::mkstemp(tmp_filename);
    if (tmp_fd < 0) {
        llvm::error_code err(errno, llvm::posix_category());
        ALOGE("Failed to create temporary file using mkstemp() for %s! (%s)",
              tmp_filename, err.message().c_str());
        delete [] tmp_filename;
        return NULL;
    }

    // Create result OutputFile. Temporary file is always truncated.
    result = new (std::nothrow) OutputFile(tmp_filename,
                                           pFlags | FileBase::kTruncate);
    if (result == NULL) {
        ALOGE("Out of memory when creates OutputFile for %s!", tmp_filename);
        // Fall through to the clean-up codes.
    } else {
        if (result->hasError()) {
            ALOGE("Failed to open temporary output file %s! (%s)",
                  result->getName().c_str(), result->getErrorMessage().c_str());
            delete result;
            result = NULL;
            // Fall through to the clean-up codes.
        }
    }

    // Clean up.
    delete [] tmp_filename;
    ::close(tmp_fd);

    return result;
}
Exemplo n.º 8
0
void writeEXRHalf(OStream *ost, const float *pixels,
	      int width, int height, int components) 
{
	//assert(components==3 || components==4);
	// TODO: throw std::exception if invalid number of components

	Header header (width, height);
	header.channels().insert ("R", Channel (HALF));
	header.channels().insert ("G", Channel (HALF));
	header.channels().insert ("B", Channel (HALF));
	if(components==4)
		header.channels().insert ("A", Channel (HALF));

	// Convert data to half
	half *data = new half [width*height*components];
	
	std::copy(pixels, pixels+(width*height*components), data);
	
	// And save it
	OutputFile file (*ost, header);
	FrameBuffer frameBuffer;

	frameBuffer.insert("R",				// name
			    Slice (HALF,		// type
				   ((char *) data)+0,	// base
				   2 * components,		// xStride
				   2 * components * width));	// yStride
	frameBuffer.insert("G",				// name
			    Slice (HALF,		// type
				   ((char *) data)+2,	// base
				   2 * components,		// xStride
				   2 * components * width));	// yStride
	frameBuffer.insert("B",				// name
			    Slice (HALF,		// type
				   ((char *) data)+4,	// base
				   2 * components,		// xStride
				   2 * components * width));	// yStride
	if(components==4) {
		frameBuffer.insert("A",					// name
				    Slice (HALF,			// type
					   ((char *) data)+6,		// base
					   2 * components,		// xStride
					   2 * components * width));	// yStride
	}

	file.setFrameBuffer(frameBuffer);
	file.writePixels(height);
	delete data;
}
Exemplo n.º 9
0
////////////////////////////////////////////////////////////////////////////////
// Saves an EXR file from Array<Rgba> data.
////////////////////////////////////////////////////////////////////////////////
static bool saveEXRFile (const char *filename, 
                          const int width, 
                          const int height, 
                          Array<Rgba>* imageData) 
{

  if (filename == NULL || imageData == NULL || width <= 0 || height <= 0) {
    printf("Cannot write EXR file: invalid filename or image data.\n");
    return false;
  }

  // prepare header
  Header header (width, height);
  header.channels().insert ("R", Channel (HALF));
  header.channels().insert ("G", Channel (HALF));
  header.channels().insert ("B", Channel (HALF));

  // create file
  OutputFile exrFile (filename, header);

  // insert frame buffer
  FrameBuffer frameBuffer;
  frameBuffer.insert ("R",									// name
    Slice (HALF,														// type
    (char *) &(((Rgba*)imageData[0])->r),		// base
    sizeof (Rgba),													// xStride
    sizeof (Rgba) * width));								// yStride

  frameBuffer.insert ("G",									// name
    Slice (HALF,														// type
    (char *) &(((Rgba*)imageData[0])->g),		// base
    sizeof (Rgba),													// xStride
    sizeof (Rgba) * width));								// yStride

  frameBuffer.insert ("B",									// name
    Slice (HALF,														// type
    (char *) &(((Rgba*)imageData[0])->b),		// base
    sizeof (Rgba),											    // xStride
    sizeof (Rgba) * width));								// yStride

  exrFile.setFrameBuffer (frameBuffer);
  exrFile.writePixels (height);

  return true;
}
Exemplo n.º 10
0
void
writeGZ2 (const char fileName[],
	  const half *gPixels,
	  const float *zPixels,
	  int width,
	  int height,
	  const Box2i &dataWindow)
{
    //
    // Write an image with only a G (green) and a Z (depth) channel,
    // using class OutputFile.  Don't store the whole image in the
    // file, but crop it according to the given data window.
    //
    //	- create a file header
    //	- set the header's data window
    //	- add G and Z channels to the header
    //	- open the file, and store the header in the file
    //	- describe the memory layout of the G anx Z pixels
    //	- store the pixels in the file
    //

    Header header (width, height);
    header.dataWindow() = dataWindow;
    header.channels().insert ("G", Channel (IMF::HALF));
    header.channels().insert ("Z", Channel (IMF::FLOAT));

    OutputFile file (fileName, header);

    FrameBuffer frameBuffer;

    frameBuffer.insert ("G",					// name
			Slice (IMF::HALF,			// type
			       (char *) gPixels,		// base
			       sizeof (*gPixels) * 1,		// xStride
			       sizeof (*gPixels) * width));	// yStride

    frameBuffer.insert ("Z",					// name
			Slice (IMF::FLOAT,			// type
			       (char *) zPixels,		// base
			       sizeof (*zPixels) * 1,		// xStride
			       sizeof (*zPixels) * width));	// yStride

    file.setFrameBuffer (frameBuffer);
    file.writePixels (dataWindow.max.y - dataWindow.min.y + 1);
}
Exemplo n.º 11
0
void ZFnEXR::saveCameraNZ(float* data, M44f mat, float fov, const char* filename, int width, int height)
{
	Header header (width, height); 
	header.insert ("fov", DoubleAttribute (fov)); 
	header.insert ("cameraTransform", M44fAttribute (mat));
	header.channels().insert ("R", Channel (FLOAT));
	
	OutputFile file (filename, header); 
	FrameBuffer frameBuffer;

	frameBuffer.insert ("R", 
						Slice (FLOAT, 
							   (char *) data, 
							   sizeof (*data) * 1, 
							   sizeof (*data) * width)); 
	file.setFrameBuffer (frameBuffer);              
	file.writePixels (height);
}
Exemplo n.º 12
0
void
makePreview (const char inFileName[],
	     const char outFileName[],
	     int previewWidth,
	     float exposure,
	     bool verbose)
{
    if (verbose)
	cout << "generating preview image" << endl;

    Array2D <PreviewRgba> previewPixels;
    int previewHeight;

    generatePreview (inFileName,
		     exposure,
		     previewWidth,
		     previewHeight,
		     previewPixels);

    InputFile in (inFileName);
    Header header = in.header();

    header.setPreviewImage
	(PreviewImage (previewWidth, previewHeight, &previewPixels[0][0]));

    if (verbose)
	cout << "copying " << inFileName << " to " << outFileName << endl;

    if (header.hasTileDescription())
    {
	TiledOutputFile out (outFileName, header);
	out.copyPixels (in);
    }
    else
    {
	OutputFile out (outFileName, header);
	out.copyPixels (in);
    }

    if (verbose)
	cout << "done." << endl;
}
Exemplo n.º 13
0
void
writeGZ1 (const char fileName[],
	  const half *gPixels,
	  const float *zPixels,
	  int width,
	  int height)
{
    //
    // Write an image with only a G (green) and a Z (depth) channel,
    // using class OutputFile.
    //
    //	- create a file header
    //	- add G and Z channels to the header
    //	- open the file, and store the header in the file
    //	- describe the memory layout of the G anx Z pixels
    //	- store the pixels in the file
    //

    Header header (width, height);
    header.channels().insert ("G", Channel (IMF::HALF));
    header.channels().insert ("Z", Channel (IMF::FLOAT));

    OutputFile file (fileName, header);

    FrameBuffer frameBuffer;

    frameBuffer.insert ("G",					// name
		        Slice (IMF::HALF,			// type
			       (char *) gPixels,		// base
			       sizeof (*gPixels) * 1,		// xStride
			       sizeof (*gPixels) * width));	// yStride

    frameBuffer.insert ("Z",					// name
			Slice (IMF::FLOAT,			// type
			       (char *) zPixels,		// base
			       sizeof (*zPixels) * 1,		// xStride
			       sizeof (*zPixels) * width));	// yStride

    file.setFrameBuffer (frameBuffer);
    file.writePixels (height);
}
Exemplo n.º 14
0
EStatusCode OpenTypeTest::SaveCharstringCode(unsigned short inFontIndex,unsigned short inGlyphIndex,CFFFileInput* inCFFFileInput)
{
	OutputFile glyphFile;

	EStatusCode status = glyphFile.OpenFile(string("C:\\PDFLibTests\\glyphCFF")  + Long(inFontIndex).ToString() + "_" + inCFFFileInput->GetGlyphName(0,inGlyphIndex) + ".txt");

	do
	{
		if(status != PDFHummus::eSuccess)
			break;
		
		CharStringType2Tracer tracer;

		status = tracer.TraceGlyphProgram(inFontIndex,inGlyphIndex,inCFFFileInput,glyphFile.GetOutputStream());

	}while(false);

	glyphFile.CloseFile();

	return status;
}
Exemplo n.º 15
0
EStatusCode PFBStreamTest::Run(const TestConfiguration& inTestConfiguration)
{
	EStatusCode status;
	InputFile pfbFile;
	OutputFile decodedPFBFile;
	InputPFBDecodeStream decodeStream;

	do
	{
		pfbFile.OpenFile(RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"TestMaterials/fonts/HLB_____.PFB"));

		decodedPFBFile.OpenFile(RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"decodedPFBFile.txt"));


		status = decodeStream.Assign(pfbFile.GetInputStream());
		
		if(status != PDFHummus::eSuccess)
		{
			cout<<"Failed to assign pfb input stream";
			break;
		}

		OutputStreamTraits traits(decodedPFBFile.GetOutputStream());

		status = traits.CopyToOutputStream(&decodeStream);

		if(status != PDFHummus::eSuccess)
		{
			cout<<"Failed to decode pfb stream";
			break;
		}
	}while(false);

	decodeStream.Assign(NULL);
	pfbFile.CloseFile();
	decodedPFBFile.CloseFile();

	return status;
}
enum Compiler::ErrorCode Compiler::compile(Script &pScript,
                                           OutputFile &pResult,
                                           llvm::raw_ostream *IRStream) {
  // Check the state of the specified output file.
  if (pResult.hasError()) {
    return kErrInvalidOutputFileState;
  }

  // Open the output file decorated in llvm::raw_ostream.
  llvm::raw_pwrite_stream *out = pResult.dup();
  if (out == nullptr) {
    return kErrPrepareOutput;
  }

  // Delegate the request.
  enum Compiler::ErrorCode err = compile(pScript, *out, IRStream);

  // Close the output before return.
  delete out;

  return err;
}
DisassembleResult Disassemble(OutputFile &pOutput, const char *pTriple,
                              const char *pFuncName, const uint8_t *pFunc,
                              size_t FuncSize) {
  // Check the state of the specified output file.
  if (pOutput.hasError()) {
    return kDisassembleInvalidOutput;
  }

  // Open the output file decorated in llvm::raw_ostream.
  llvm::raw_ostream *output = pOutput.dup();
  if (output == NULL) {
    return kDisassembleFailedPrepareOutput;
  }

  // Delegate the request.
  DisassembleResult result =
      Disassemble(*output, pTriple, pFuncName, pFunc, FuncSize);

  // Close the output before return.
  delete output;

  return result;
}
Exemplo n.º 18
0
EStatusCode Type1Test::SaveCharstringCode(const TestConfiguration& inTestConfiguration,const string& inCharStringName,Type1Input* inType1Input)
{
	OutputFile glyphFile;

    
	EStatusCode status = glyphFile.OpenFile(
                                            RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,string("glyphType1_") + inCharStringName + "_.txt"));

	do
	{
		if(status != PDFHummus::eSuccess)
			break;
		
		CharStringType1Tracer tracer;

		status = tracer.TraceGlyphProgram(inCharStringName,inType1Input,glyphFile.GetOutputStream());

	}while(false);

	glyphFile.CloseFile();

	return status;

}
Exemplo n.º 19
0
auto_ptr<AlignmentSteps> RootedClusterTree::treeFromDistMatrix(RootedGuideTree* phyloTree,DistMatrix* distMat, Alignment *alignPtr,
                                           int seq1, int nSeqs, string& phylipName)
{
    OutputFile phylipPhyTreeFile;
    auto_ptr<AlignmentSteps> progSteps;
    try
    {
        // Test to see if the inputs are valid
        if(seq1 < 1 || nSeqs < 1)
        {
            cerr << "Invalid inputs into treeFromDistMatrix \n"
                 << "seq1 = " << seq1 << " nSeqs = " << nSeqs << "\n"
                 << "Need to end program!\n";
            exit(1);
            return progSteps;
        }

        float dist;
        string path;
        verbose = false;
        firstSeq = seq1;
        lastSeq = firstSeq + nSeqs - 1;

        SeqInfo info;
        info.firstSeq = firstSeq;
        info.lastSeq = lastSeq;
        info.numSeqs = nSeqs;

        utilityObject->getPath(userParameters->getSeqName(), &path);

        if(nSeqs >= 2)
        {
            string name = phylipName;
            if(!phylipPhyTreeFile.openFile(&name,
                             "\nEnter name for new GUIDE TREE           file  ", &path, "dnd",
                             "Guide tree"))
            {
                return progSteps;
            }
            phylipName = name;
        }
        else
        {
            return progSteps;
        }

        RootedTreeOutput outputTree(&info);

        ofstream* ptrToFile = phylipPhyTreeFile.getPtrToFile();

        if (nSeqs == 2)
        {
            dist = (*distMat)(firstSeq, firstSeq + 1) / 2.0;
            if(ptrToFile->is_open())
            {
                (*ptrToFile) <<  "(" << alignPtr->getName(firstSeq) << ":"
                             << setprecision(5)
                             << dist << "," << alignPtr->getName(firstSeq + 1) << ":"
                             << setprecision(5) << dist <<");\n";
            }
            progSteps.reset(new AlignmentSteps);
            vector<int> groups;
            groups.resize(nSeqs + 1, 0);
            groups[1] = 1;
            groups[2] = 2;
        }
        else
        {
            UPGMAAlgorithm clusAlgorithm;
            progSteps = clusAlgorithm.generateTree(phyloTree, distMat, &info, false);
            outputTree.printPhylipTree(phyloTree, ptrToFile, alignPtr, distMat);
        }
        return progSteps;
    }
    catch(const exception &ex)
    {
        cerr << "ERROR: Error has occured in treeFromDistMatrix. "
             << "Need to terminate program.\n"
             << ex.what();
        exit(1);
    }
    catch(...)
    {
        cerr << "ERROR: Error has occured in treeFromDistMatrix. "
             << "Need to terminate program.\n";
        exit(1);
    }
}
Exemplo n.º 20
0
void RootedClusterTree::treeFromAlignment(TreeNames* treeNames, Alignment *alignPtr)
{
    try
    {
        OutputFile phylipPhyTreeFile;
        OutputFile clustalPhyTreeFile;
        OutputFile distancesPhyTreeFile;
        OutputFile nexusPhyTreeFile;
        OutputFile pimFile;

        RootedGuideTree phyloTree;

        string path;
        int j;
        int overspill = 0;
        int totalDists;
        numSeqs = alignPtr->getNumSeqs(); // NOTE class variable

        /**
         * Check if numSeqs is ok
         */
        if(!checkIfConditionsMet(numSeqs, 2))
        {
            return;
        }

        firstSeq = 1;
        lastSeq = numSeqs;

        // The SeqInfo struct is passed to reduce the number of parameters passed!
        SeqInfo info;
        info.firstSeq = firstSeq;
        info.lastSeq = lastSeq;
        info.numSeqs = numSeqs;

        RootedTreeOutput outputTree(&info); // No bootstrap!

        utilityObject->getPath(userParameters->getSeqName(), &path);

        /**
         * Open the required output files.
         */
        if(!openFilesForTreeFromAlignment(&clustalPhyTreeFile, &phylipPhyTreeFile,
                        &distancesPhyTreeFile, &nexusPhyTreeFile, &pimFile, treeNames, &path))
        {
            return; // Problem opeing one of the files, cannot continue!
        }

        int _lenFirstSeq = alignPtr->getSeqLength(firstSeq);

        bootPositions.clear();
        bootPositions.resize(_lenFirstSeq + 2);

        for (j = 1; j <= _lenFirstSeq; ++j)
        {
            bootPositions[j] = j;
        }

        /**
         * Calculate quickDist and overspill
         */
        overspill = calcQuickDistMatForAll(clustalPhyTreeFile.getPtrToFile(),
                       phylipPhyTreeFile.getPtrToFile(), nexusPhyTreeFile.getPtrToFile(),
                       pimFile.getPtrToFile(), distancesPhyTreeFile.getPtrToFile(), alignPtr);

        // check if any distances overflowed the distance corrections
        if (overspill > 0)
        {
            totalDists = (numSeqs *(numSeqs - 1)) / 2;
            overspillMessage(overspill, totalDists);
        }

        if (userParameters->getOutputTreeClustal())
        {
            verbose = true;
        }
        // Turn on file output


        if (userParameters->getOutputTreeClustal() ||
            userParameters->getOutputTreePhylip()
            || userParameters->getOutputTreeNexus())
        {
            UPGMAAlgorithm clusAlgorithm;
            clusAlgorithm.setVerbose(true);
            clusAlgorithm.generateTree(&phyloTree, quickDistMat.get(), &info, false,
                                       clustalPhyTreeFile.getPtrToFile());
            clusAlgorithm.setVerbose(false);
        }

        if (userParameters->getOutputTreePhylip())
        {
            outputTree.printPhylipTree(&phyloTree, phylipPhyTreeFile.getPtrToFile(), alignPtr,
                                        quickDistMat.get());
        }

        if (userParameters->getOutputTreeNexus())
        {
            outputTree.printNexusTree(&phyloTree, nexusPhyTreeFile.getPtrToFile(), alignPtr,
                                       quickDistMat.get());
        }

        /** Free up resources!!!!! */

        treeGaps.clear();
        bootPositions.clear();

    }
    catch(const exception& ex)
    {
        cerr << ex.what() << endl;
        utilityObject->error("Terminating program. Cannot continue\n");
        exit(1);
    }
}
Exemplo n.º 21
0
void GPUOctree::dumpIndirection(const char *filename)
{
	m_idr = new short[INDIRECTIONPOOLSIZE*4];
	m_dt = new float[DATAPOOLSIZE*4];

	setIndirection(m_root);
	
	half *idr_r = new half[INDIRECTIONPOOLSIZE];
	half *idr_g = new half[INDIRECTIONPOOLSIZE];
	half *idr_b = new half[INDIRECTIONPOOLSIZE];
	half *idr_a = new half[INDIRECTIONPOOLSIZE];
	
	for(long i=0; i<INDIRECTIONPOOLSIZE; i++) {
		idr_r[i] = (half)m_idr[i*4];
		idr_g[i] = (half)m_idr[i*4+1];
		idr_b[i] = (half)m_idr[i*4+2];
		idr_a[i] = (half)m_idr[i*4+3];
	}
	
// save indirection	
	Header idrheader (INDIRECTIONPOOLWIDTH, INDIRECTIONPOOLWIDTH); 
	idrheader.insert ("root_size", FloatAttribute (m_rootSize));
	idrheader.insert ("root_center", V3fAttribute (Imath::V3f(m_rootCenter.x, m_rootCenter.y, m_rootCenter.z))); 

		idrheader.channels().insert ("R", Channel (HALF));
		idrheader.channels().insert ("G", Channel (HALF));                                   // 1 
        idrheader.channels().insert ("B", Channel (HALF));
		idrheader.channels().insert ("A", Channel (HALF));                   // 2  
    
	std::string idrname = filename;
	idrname += ".idr";
        OutputFile idrfile (idrname.c_str(), idrheader);                               // 4 
        FrameBuffer idrframeBuffer;
		 idrframeBuffer.insert ("R",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) idr_r,            // base   // 8 
                                   sizeof (*idr_r) * 1,       // xStride// 9 
                                   sizeof (*idr_r) * INDIRECTIONPOOLWIDTH));
        idrframeBuffer.insert ("G",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) idr_g,            // base   // 8 
                                   sizeof (*idr_g) * 1,       // xStride// 9 
                                   sizeof (*idr_g) * INDIRECTIONPOOLWIDTH));
		 idrframeBuffer.insert ("B",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) idr_b,            // base   // 8 
                                   sizeof (*idr_b) * 1,       // xStride// 9 
                                   sizeof (*idr_b) * INDIRECTIONPOOLWIDTH));
		 idrframeBuffer.insert ("A",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) idr_a,            // base   // 8 
                                   sizeof (*idr_a) * 1,       // xStride// 9 
                                   sizeof (*idr_a) * INDIRECTIONPOOLWIDTH));
       
        idrfile.setFrameBuffer (idrframeBuffer);                                // 16 
        idrfile.writePixels (INDIRECTIONPOOLWIDTH); 
        
        delete[] idr_r;
	delete[] idr_g;
	delete[] idr_b;
	delete[] idr_a;
// save data

	half *dt_r = new half[DATAPOOLSIZE];
	half *dt_g = new half[DATAPOOLSIZE];
	half *dt_b = new half[DATAPOOLSIZE];
	half *dt_a = new half[DATAPOOLSIZE];
	
	for(long i=0; i<DATAPOOLSIZE; i++) {
		dt_r[i] = (half)m_dt[i*4];
		dt_g[i] = (half)m_dt[i*4+1];
		dt_b[i] = (half)m_dt[i*4+2];
		dt_a[i] = (half)m_dt[i*4+3];
	}
	
	Header dtheader (DATAPOOLWIDTH, DATAPOOLWIDTH); 
		dtheader.channels().insert ("R", Channel (HALF));
		dtheader.channels().insert ("G", Channel (HALF));                                   // 1 
        dtheader.channels().insert ("B", Channel (HALF));
		dtheader.channels().insert ("A", Channel (HALF));                   // 2  
    
	std::string dtname = filename;
	dtname += ".exr";
        OutputFile dtfile (dtname.c_str(), dtheader);                               // 4 
        FrameBuffer dtframeBuffer;
		 dtframeBuffer.insert ("R",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) dt_r,            // base   // 8 
                                   sizeof (*dt_r) * 1,       // xStride// 9 
                                   sizeof (*dt_r) * DATAPOOLWIDTH));
        dtframeBuffer.insert ("G",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) dt_g,            // base   // 8 
                                   sizeof (*dt_g) * 1,       // xStride// 9 
                                   sizeof (*dt_g) * DATAPOOLWIDTH));
		 dtframeBuffer.insert ("B",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) dt_b,            // base   // 8 
                                   sizeof (*dt_b) * 1,       // xStride// 9 
                                   sizeof (*dt_b) * DATAPOOLWIDTH));
		 dtframeBuffer.insert ("A",                                // name   // 6 
                            Slice (HALF,                        // type   // 7 
                                   (char *) dt_a,            // base   // 8 
                                   sizeof (*dt_a) * 1,       // xStride// 9 
                                   sizeof (*dt_a) * DATAPOOLWIDTH));
       
        dtfile.setFrameBuffer (dtframeBuffer);                                // 16 
        dtfile.writePixels (DATAPOOLWIDTH); 
		
	delete[] dt_r;
	delete[] dt_g;
	delete[] dt_b;
	delete[] dt_a;
}
Exemplo n.º 22
0
void
makeMultiView (const vector <string> &viewNames,
	       const vector <const char *> &inFileNames,
	       const char *outFileName,
	       Compression compression,
	       bool verbose)
{
    Header header;
    Image image;
    FrameBuffer outFb;

    //
    // Find the size of the dataWindow, check files
    //
    
    Box2i d;
    
    
    for (int i = 0; i < viewNames.size(); ++i)
    {
	InputFile in (inFileNames[i]);

	if (verbose)
	{
	    cout << "reading file " << inFileNames[i] << " "
		    "for " << viewNames[i] << " view" << endl;
	}

	if (hasMultiView (in.header()))
	{
	    THROW (IEX_NAMESPACE::NoImplExc,
		   "The image in file " << inFileNames[i] << " is already a "
		   "multi-view image.  Cannot combine multiple multi-view "
		   "images.");
	}

        header = in.header();
	if (i == 0)
        {
             d=header.dataWindow();
	}else{
             d.extendBy(header.dataWindow());
        }
    }
    
    
    image.resize (d);
    
    header.dataWindow()=d;
    
    // blow away channels; we'll rebuild them
    header.channels()=ChannelList();
    
    
    //
    // Read the input image files
    //

    for (int i = 0; i < viewNames.size(); ++i)
    {
	InputFile in (inFileNames[i]);

	if (verbose)
	{
	    cout << "reading file " << inFileNames[i] << " "
		    "for " << viewNames[i] << " view" << endl;
	}

	FrameBuffer inFb;

	for (ChannelList::ConstIterator j = in.header().channels().begin();
	     j != in.header().channels().end();
	     ++j)
	{
	    const Channel &inChannel = j.channel();
	    string inChanName = j.name();
	    string outChanName = insertViewName (inChanName, viewNames, i);

	    image.addChannel (outChanName, inChannel);
            image.channel(outChanName).black();
            
	    header.channels().insert (outChanName, inChannel);

	    inFb.insert  (inChanName,  image.channel(outChanName).slice());
	    outFb.insert (outChanName, image.channel(outChanName).slice());
	}

	in.setFrameBuffer (inFb);
	in.readPixels (in.header().dataWindow().min.y, in.header().dataWindow().max.y);
    }

    //
    // Write the output image file
    //

    {
	header.compression() = compression;
	addMultiView (header, viewNames);

	OutputFile out (outFileName, header);

	if (verbose)
	    cout << "writing file " << outFileName << endl;

	out.setFrameBuffer (outFb);

	out.writePixels
	    (header.dataWindow().max.y - header.dataWindow().min.y + 1);
    }
}
// Write C code for corresponding to the feed forward network into a given file.
void MultipleBackPropagation::GenerateCCode(OutputFile & f, VariablesData & trainVariables, BOOL inputLayerIsConnectedWithOutputLayer, BOOL spaceInputLayerIsConnectedWithOutputLayer) {
	CString s;

	CString MBPVersion;
	MBPVersion.LoadString(IDS_VERSION);

	f.WriteLine("/**");
	f.WriteLine(_TEXT(" Generated by ") + MBPVersion);
	f.WriteLine(" Multiple Back-Propagation can be freely obtained at http://dit.ipg.pt/MBP");
	f.WriteLine("*/\n");

	f.WriteLine("#include <math.h>");
	
	f.WriteLine("/**");
	s.Format(_TEXT(" inputs  - should be an array of %d element(s), containing the network input(s)."), inputs);

	bool hasMissingValues = false;
	for(int i = 0; i < inputs; i++) {
		if (trainVariables.HasMissingValues(i)) {
			s += " Inputs with NaN value are considered missing values.";
			hasMissingValues = true;
			break;
		}
	}

	f.WriteLine(s);
		
	s.Format(_TEXT(" outputs - should be an array of %d element(s), that will contain the network output(s)."), outputs);
	f.WriteLine(s);
	s = " Note : The array inputs will also be changed.";
	if (!hasMissingValues) s += "Its values will be rescaled between -1 and 1.";
	s += "\n*/";
	f.WriteLine(s);

	s = f.GetFileName();
	int p = s.Find('.');
	if (p != -1) s = s.Left(p);
	f.WriteLine(_TEXT("void ") +  s + _TEXT("(double * inputs, double * outputs) {"));

	f.WriteString("\tdouble mainWeights[] = {");
	SaveWeights(f, ", ");
	f.WriteLine("};");
	f.WriteLine("\tdouble * mw = mainWeights;");
	if (hasMissingValues) f.WriteLine("\tdouble b;");

	if (!spaceNetwork.IsNull()) {
		f.WriteString("\tdouble spaceWeights[] = {");
		spaceNetwork->SaveWeights(f, ", ");
		f.WriteLine("};");
		f.WriteLine("\tdouble * sw = spaceWeights;");
		s.Format(_TEXT("\tdouble mk[%d];"), spaceNetwork->Outputs());
		f.WriteLine(s);
		f.WriteLine("\tdouble *m = mk;");
		
		if (hasMissingValues) {
			s.Format(L"\tdouble spaceInputs[%d];", inputs);
			f.WriteLine(s);
		}
	}

	int numberLayers = layers.Lenght();

	for (int l = 1; l < numberLayers - 1; l++) {
		s.Format(L"\tdouble hiddenLayer%doutputs[%d];", l, layers.Element(l)->neurons.Lenght());
		f.WriteLine(s);
	}

	int numberSpaceLayers = (spaceNetwork.IsNull()) ? 0 : spaceNetwork->layers.Lenght();

	for (int l = 1; l < numberSpaceLayers - 1; l++) {
		s.Format(_TEXT("\tdouble spaceHiddenLayer%doutputs[%d];"), l, spaceNetwork->layers.Element(l)->neurons.Lenght());
		f.WriteLine(s);
	}

	f.WriteLine("\tint c;");

	f.WriteString("\n");

	// input variables will be rescaled between -1 and 1
	if (trainVariables.Number() == inputs + outputs) {
		for (int i = 0; i < inputs; i++) { 
			double min = trainVariables.Minimum(i);
			double max = trainVariables.Maximum(i);

			if (trainVariables.HasMissingValues(i)) {
				s.Format(L"\tif(inputs[%d] == inputs[%d]) { /* compiler must have support for NaN numbers */\n\t", i, i);
				f.WriteString(s);
			}

			if (min != max) {
				s.Format(L"\tinputs[%d] = -1.0 + (inputs[%d] - %1.15f) / %1.15f;", i, i, min, (max - min)/2);				
			} else {
				s.Format(L"\tinputs[%d] = inputs[%d] / %1.15f; /* WARNING: During the training this variable remain always constant */", i, i, max);
			}
			f.WriteLine(s);

			if (hasMissingValues && !spaceNetwork.IsNull()) {
				if (trainVariables.HasMissingValues(i)) f.WriteString("\t");
				s.Format(L"\tspaceInputs[%d] = inputs[%d];", i, i);
				f.WriteLine(s);
			}

			if (trainVariables.HasMissingValues(i)) {
				if (!spaceNetwork.IsNull()) {
					f.WriteLine("\t\tb = *sw++;");
					s.Format(L"\t\tspaceInputs[%d] = tanh(b + spaceInputs[%d] * *sw++);", i, i);
					f.WriteLine(s);
				}

				f.WriteLine("\t\tb = *mw++;");
				s.Format(L"\t\tinputs[%d] = tanh(b + inputs[%d] * *mw++);", i, i);
				f.WriteLine(s);
				f.WriteLine("\t} else {");
				s.Format(L"\t\tspaceInputs[%d] = inputs[%d] = 0.0;", i, i);
				f.WriteLine(s);
				f.WriteLine("\t}");
			}
		}
	}

	// space network
	for (int l = 1; l < numberSpaceLayers; l++) {
		List<Neuron> * neurons = &(spaceNetwork->layers.Element(l)->neurons);

		int nn = 0;
		for(NeuronWithInputConnections * n = dynamic_cast<NeuronWithInputConnections *>(neurons->First()); n != NULL; n = dynamic_cast<NeuronWithInputConnections *>(neurons->Next())) {
			CString aux;

			if (l == numberSpaceLayers -1) {
				aux.Format(_TEXT("mk[%d]"), nn);
			} else {
				aux.Format(_TEXT("spaceHiddenLayer%doutputs[%d]"), l, nn);
			}

			f.WriteString("\t");
			f.WriteString(aux);
			f.WriteLine(" = *sw++;");

			int numberInputsFromLastLayer = n->inputs.Lenght() - 1; // - bias
			if (spaceInputLayerIsConnectedWithOutputLayer  && l == numberSpaceLayers -1) numberInputsFromLastLayer -= inputs;

			s.Format(_TEXT("\tfor(c = 0; c < %d; c++) "), numberInputsFromLastLayer);
			f.WriteString(s);
			f.WriteString(aux);
			f.WriteString(" += *sw++ * ");

			if (l == 1) {
				s = (hasMissingValues) ? "spaceI" : "i";
				s+= "nputs[c];";
			} else {
				s.Format(_TEXT("spaceHiddenLayer%doutputs[c];"), l-1);
			}
			f.WriteLine(s);

			if (spaceInputLayerIsConnectedWithOutputLayer  && l == numberSpaceLayers -1) {
				s.Format(_TEXT("\tfor(c = 0; c < %d; c++) "), inputs);
				f.WriteString(s);
				f.WriteLine(aux + L" += *sw++ * " + ((hasMissingValues) ? "spaceI" : "i") + "nputs[c];");
			}

			WriteActivationFunctionCCode(f, n, CT2CA(aux));

			nn++;
		}
	}

	// main network

	for (int l = 1; l < numberLayers; l++) {
		List<Neuron> * neurons = &(layers.Element(l)->neurons);

		int nn = 0;
		for(NeuronWithInputConnections * n = dynamic_cast<NeuronWithInputConnections *>(neurons->First()); n != NULL; n = dynamic_cast<NeuronWithInputConnections *>(neurons->Next())) {
			CString aux;

			if (l == numberLayers -1) {
				aux.Format(_TEXT("outputs[%d]"), nn);
			} else {
				aux.Format(_TEXT("hiddenLayer%doutputs[%d]"), l, nn);
			}

			f.WriteString("\t");
			f.WriteString(aux);
			f.WriteLine(" = *mw++;");
			
			int numberInputsFromLastLayer = n->inputs.Lenght() - 1; // - bias
			if (inputLayerIsConnectedWithOutputLayer && l == numberLayers -1) numberInputsFromLastLayer -= inputs;

			s.Format(_TEXT("\tfor(c = 0; c < %d; c++) "), numberInputsFromLastLayer);
			f.WriteString(s);
			f.WriteString(aux);
			f.WriteString(" += *mw++ * ");

			if (l == 1) {
				s = "inputs[c];";
			} else {
				s.Format(_TEXT("hiddenLayer%doutputs[c];"), l-1);
			}

			f.WriteLine(s);

			if (inputLayerIsConnectedWithOutputLayer && l == numberLayers -1) {
				s.Format(_TEXT("\tfor(c = 0; c < %d; c++) "), inputs);
				f.WriteString(s);
				f.WriteLine(aux + _TEXT(" += *mw++ * inputs[c];"));
			}

			WriteActivationFunctionCCode(f, n, CT2CA(aux));

			if (!spaceNetwork.IsNull() && nn < neuronsWithSelectiveActivation[l]) {
				f.WriteString("\t");
				f.WriteString(aux);
				f.WriteLine(" *= *m++;");
			}

			nn++;
		}
	}

	// Rescale the outputs
	if (trainVariables.Number() == inputs + outputs) {
		for (int o = 0; o < outputs; o++) {
			int outVar = o + inputs;

			double min = trainVariables.Minimum(outVar);
			double max = trainVariables.Maximum(outVar);
			double nmin = trainVariables.newMinimum[outVar];

			s.Format(_TEXT("\toutputs[%d] = %1.15f + (outputs[%d] - %f) * %1.15f;"), o, min, o, nmin, (max - min) / (1.0 - nmin));
			f.WriteLine(s);
		}
	}

	f.WriteLine("}");
}
Exemplo n.º 24
0
static int imb_save_openexr_half(struct ImBuf *ibuf, const char *name, int flags)
{
	int channels = ibuf->channels;
	int width = ibuf->x;
	int height = ibuf->y;
	int write_zbuf = (flags & IB_zbuffloat) && ibuf->zbuf_float != NULL;   // summarize
	
	try
	{
		Header header (width, height);
		
		openexr_header_compression(&header, ibuf->ftype & OPENEXR_COMPRESS);
		openexr_header_metadata(&header, ibuf);
		
		header.channels().insert ("R", Channel (HALF));
		header.channels().insert ("G", Channel (HALF));
		header.channels().insert ("B", Channel (HALF));
		if (ibuf->depth==32 && channels >= 4)
			header.channels().insert ("A", Channel (HALF));
		if (write_zbuf)		// z we do as float always
			header.channels().insert ("Z", Channel (FLOAT));
		
		FrameBuffer frameBuffer;			
		OutputFile *file = new OutputFile(name, header);			
		
		/* we store first everything in half array */
		RGBAZ *pixels = new RGBAZ[height * width];
		RGBAZ *to = pixels;
		int xstride= sizeof (RGBAZ);
		int ystride= xstride*width;

		/* indicate used buffers */
		frameBuffer.insert ("R", Slice (HALF,  (char *) &pixels[0].r, xstride, ystride));	
		frameBuffer.insert ("G", Slice (HALF,  (char *) &pixels[0].g, xstride, ystride));
		frameBuffer.insert ("B", Slice (HALF,  (char *) &pixels[0].b, xstride, ystride));
		if (ibuf->depth==32 && channels >= 4)
			frameBuffer.insert ("A", Slice (HALF, (char *) &pixels[0].a, xstride, ystride));
		if (write_zbuf)
			frameBuffer.insert ("Z", Slice (FLOAT, (char *)(ibuf->zbuf_float + (height-1)*width),
											sizeof(float), sizeof(float) * -width));
		if(ibuf->rect_float) {
			float *from;

/* OCIO TODO: do this before save in BKE image.c where colormanagement is available */
//			if(ibuf->profile == IB_PROFILE_LINEAR_RGB) {
				for (int i = ibuf->y-1; i >= 0; i--)
				{
					from= ibuf->rect_float + channels*i*width;

					for (int j = ibuf->x; j > 0; j--)
					{
						to->r = from[0];
						to->g = from[1];
						to->b = from[2];
						to->a = (channels >= 4)? from[3]: 1.0f;
						to++; from += 4;
					}
				}
//			}
//			else {
//				for (int i = ibuf->y-1; i >= 0; i--)
//				{
//					from= ibuf->rect_float + channels*i*width;

//					for (int j = ibuf->x; j > 0; j--)
//					{
//						to->r = srgb_to_linearrgb(from[0]);
//						to->g = srgb_to_linearrgb(from[1]);
//						to->b = srgb_to_linearrgb(from[2]);
//						to->a = (channels >= 4)? from[3]: 1.0f;
//						to++; from += 4;
//					}
//				}
//			}
		}
		else {
			unsigned char *from;

//			if(ibuf->profile == IB_PROFILE_LINEAR_RGB) {
				for (int i = ibuf->y-1; i >= 0; i--)
				{
					from= (unsigned char *)ibuf->rect + channels*i*width;

					for (int j = ibuf->x; j > 0; j--)
					{
						to->r = (float)(from[0])/255.0;
						to->g = (float)(from[1])/255.0;
						to->b = (float)(from[2])/255.0;
						to->a = (float)(channels >= 4) ? from[3]/255.0 : 1.0f;
						to++; from += 4;
					}
				}
//			}
//			else {
//				for (int i = ibuf->y-1; i >= 0; i--)
//				{
//					from= (unsigned char *)ibuf->rect + channels*i*width;

//					for (int j = ibuf->x; j > 0; j--)
//					{
//						to->r = srgb_to_linearrgb((float)from[0] / 255.0);
//						to->g = srgb_to_linearrgb((float)from[1] / 255.0);
//						to->b = srgb_to_linearrgb((float)from[2] / 255.0);
//						to->a = channels >= 4 ? (float)from[3]/255.0 : 1.0f;
//						to++; from += 4;
//					}
//				}
//			}
		}
		
//		printf("OpenEXR-save: Writing OpenEXR file of height %d.\n", height);
		
		file->setFrameBuffer (frameBuffer);				  
		file->writePixels (height);					  
		delete file;
		delete [] pixels;
	}
	catch (const std::exception &exc)
	{      
		printf("OpenEXR-save: ERROR: %s\n", exc.what());
		if (ibuf) IMB_freeImBuf(ibuf);
		
		return (0);
	}
	
	return (1);
}
Exemplo n.º 25
0
Arquivo: work.cpp Projeto: tfauck/upx
void do_one_file(const char *iname, char *oname)
{
    int r;
    struct stat st;
    memset(&st, 0, sizeof(st));
#if (HAVE_LSTAT)
    r = lstat(iname, &st);
#else
    r = stat(iname, &st);
#endif
    if (r != 0)
    {
        if (errno == ENOENT)
            throw FileNotFoundException(iname, errno);
        else
            throwIOException(iname, errno);
    }
    if (!(S_ISREG(st.st_mode)))
        throwIOException("not a regular file -- skipped");
#if defined(__unix__)
    // no special bits may be set
    if ((st.st_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0)
        throwIOException("file has special permissions -- skipped");
#endif
    if (st.st_size <= 0)
        throwIOException("empty file -- skipped");
    if (st.st_size < 512)
        throwIOException("file is too small -- skipped");
    if (!mem_size_valid_bytes(st.st_size))
        throwIOException("file is too large -- skipped");
    if ((st.st_mode & S_IWUSR) == 0)
    {
        bool skip = true;
        if (opt->output_name)
            skip = false;
        else if (opt->to_stdout)
            skip = false;
        else if (opt->backup)
            skip = false;
        if (skip)
            throwIOException("file is write protected -- skipped");
    }

    InputFile fi;
    fi.st = st;
    fi.sopen(iname, O_RDONLY | O_BINARY, SH_DENYWR);

#if (USE_FTIME)
    struct ftime fi_ftime;
    memset(&fi_ftime, 0, sizeof(fi_ftime));
    if (opt->preserve_timestamp)
    {
        if (getftime(fi.getFd(), &fi_ftime) != 0)
            throwIOException("cannot determine file timestamp");
    }
#endif

    // open output file
    OutputFile fo;
    if (opt->cmd == CMD_COMPRESS || opt->cmd == CMD_DECOMPRESS)
    {
        if (opt->to_stdout)
        {
            if (!fo.openStdout(1, opt->force ? true : false))
                throwIOException("data not written to a terminal; Use '-f' to force.");
        }
        else
        {
            char tname[ACC_FN_PATH_MAX+1];
            if (opt->output_name)
                strcpy(tname,opt->output_name);
            else
            {
                if (!maketempname(tname, sizeof(tname), iname, ".upx"))
                    throwIOException("could not create a temporary file name");
            }
            if (opt->force >= 2)
            {
#if (HAVE_CHMOD)
                r = chmod(tname, 0777);
                IGNORE_ERROR(r);
#endif
                r = unlink(tname);
                IGNORE_ERROR(r);
            }
            int flags = O_CREAT | O_WRONLY | O_BINARY;
            if (opt->force)
                flags |= O_TRUNC;
            else
                flags |= O_EXCL;
            int shmode = SH_DENYWR;
#if defined(__MINT__)
            flags |= O_TRUNC;
            shmode = O_DENYRW;
#endif
            // cannot rely on open() because of umask
            //int omode = st.st_mode | 0600;
            int omode = 0600;
            if (!opt->preserve_mode)
                omode = 0666;
            fo.sopen(tname,flags,shmode,omode);
            // open succeeded - now set oname[]
            strcpy(oname,tname);
        }
    }

    // handle command
    PackMaster pm(&fi, opt);
    if (opt->cmd == CMD_COMPRESS)
        pm.pack(&fo);
    else if (opt->cmd == CMD_DECOMPRESS)
        pm.unpack(&fo);
    else if (opt->cmd == CMD_TEST)
        pm.test();
    else if (opt->cmd == CMD_LIST)
        pm.list();
    else if (opt->cmd == CMD_FILEINFO)
        pm.fileInfo();
    else
        throwInternalError("invalid command");

    // copy time stamp
    if (opt->preserve_timestamp && oname[0] && fo.isOpen())
    {
#if (USE_FTIME)
        r = setftime(fo.getFd(), &fi_ftime);
        IGNORE_ERROR(r);
#elif (USE__FUTIME)
        struct _utimbuf u;
        u.actime = st.st_atime;
        u.modtime = st.st_mtime;
        r = _futime(fo.getFd(), &u);
        IGNORE_ERROR(r);
#endif
    }

    // close files
    fo.closex();
    fi.closex();

    // rename or delete files
    if (oname[0] && !opt->output_name)
    {
        // FIXME: .exe or .cof etc.
        if (!opt->backup)
        {
#if (HAVE_CHMOD)
            r = chmod(iname, 0777);
            IGNORE_ERROR(r);
#endif
            File::unlink(iname);
        }
        else
        {
            // make backup
            char bakname[ACC_FN_PATH_MAX+1];
            if (!makebakname(bakname, sizeof(bakname), iname))
                throwIOException("could not create a backup file name");
            File::rename(iname,bakname);
        }
        File::rename(oname,iname);
    }

    // copy file attributes
    if (oname[0])
    {
        oname[0] = 0;
        const char *name = opt->output_name ? opt->output_name : iname;
        UNUSED(name);
#if (USE_UTIME)
        // copy time stamp
        if (opt->preserve_timestamp)
        {
            struct utimbuf u;
            u.actime = st.st_atime;
            u.modtime = st.st_mtime;
            r = utime(name, &u);
            IGNORE_ERROR(r);
        }
#endif
#if (HAVE_CHMOD)
        // copy permissions
        if (opt->preserve_mode)
        {
            r = chmod(name, st.st_mode);
            IGNORE_ERROR(r);
        }
#endif
#if (HAVE_CHOWN)
        // copy the ownership
        if (opt->preserve_ownership)
        {
            r = chown(name, st.st_uid, st.st_gid);
            IGNORE_ERROR(r);
        }
#endif
    }

    UiPacker::uiConfirmUpdate();
}
Exemplo n.º 26
0
Arquivo: main.cpp Projeto: ysei/cpu
int main(int argc, char **argv)
{
    int err;

    // read in any overriding configuration from the command line
    for(;;) {
        int c;
        int option_index = 0;

        static struct option long_options[] = {
            {"output", 1, 0, 'o'},
            {0, 0, 0, 0},
        };
        
        c = getopt_long(argc, argv, "o:", long_options, &option_index);
        if(c == -1)
            break;

        switch(c) {
            case 'o':
                output_filename = optarg;
                break;
            default:
                usage(argc, argv);
                break;
        }
    }

    if (argc - optind < 1) {
        usage(argc, argv);
        return 1;
    }

    argc -= optind;
    argv += optind;

    // start preprocessor
    int preprocess_out = preprocess(argv[0]);
    if (preprocess_out < 0) {
        fprintf(stderr, "error starting preprocessor\n");
        return 1;
    }

    if (open_input(preprocess_out, argv[0]) < 0) {
        fprintf(stderr, "error opening input file\n");
        return 1;
    }

    if (output_filename == "") {
        // build one out of the input file
        output_filename = std::string(argv[0]) + ".bin";
        printf("output file %s\n", output_filename.c_str());
    }

    OutputFile *f = new OutputFile();
    if (f->OpenFile(output_filename) < 0) {
        fprintf(stderr, "error opening output file\n");
        return 1;
    }

    gSymtab = new Symtab();
    gCodegen = new Codegen();
    gCodegen->InitSymtab(gSymtab);
    gCodegen->SetOutput(f);

    err = parse_source();
    if (err < 0)
        goto err;
    
    gCodegen->FixupPass();

err:
    close(preprocess_out);
    delete gCodegen;
    delete gSymtab;
    delete f;

    return err;
}
/**
	Method   : void WriteActivationFunctionCCode(OutputFile & f, NeuronWithInputConnections * n, const char * outputNeuronVariable)
	Purpose  : Write C code for the activation function of a given neuron.
	Version  : 1.0.1
*/
void MultipleBackPropagation::WriteActivationFunctionCCode(OutputFile & f, NeuronWithInputConnections * n, const char * outputNeuronVariable) {
	CString s;

	ActivationFunction * a = (ActivationFunction *) (n->function);

	if (a->Alpha() == 1 && a->id == Linear) return;

	f.WriteString("\t");
	f.WriteString(outputNeuronVariable);

	switch (a->id) {
		case Sigmoid :
			f.WriteString(" = 1.0 / (1.0 + exp(");
			if (a->Alpha() == 1.0) {
				f.WriteString("-");
			} else {
				s.Format(_TEXT("%1.15f"), -a->Alpha());
				f.WriteString(s);
				f.WriteString(" * ");
			}
			f.WriteString(outputNeuronVariable);
			f.WriteLine("));");
			break;
		case Tanh :
			f.WriteString(" = tanh(");
			if (a->Alpha() != 1.0) {
				s.Format(_TEXT("%1.15f"), a->Alpha());
				f.WriteString(s);
				f.WriteString(" * ");
			}
			f.WriteString(outputNeuronVariable);
			f.WriteLine(");");
			break;
		case Gaussian :
			f.WriteString(" = exp(-(");
			f.WriteString(outputNeuronVariable);
			f.WriteString(" * ");
			f.WriteString(outputNeuronVariable);
			f.WriteString(")");
			if (a->Alpha() != 1.0) {
				f.WriteString(" / ");
				s.Format(_TEXT("%1.15f"), a->Alpha());
				f.WriteString(s);
			}
			f.WriteLine(");");
			break;
		default : // linear	
			if (a->Alpha() != 1.0) {
				s.Format(_TEXT(" *= %1.15f"), a->Alpha());
				f.WriteString(s);
				f.WriteLine(";");
			}
	}
}
Exemplo n.º 28
0
EStatusCode CustomLogTest::Run()
{
	// Place log in a compressed stream, for a non-file PDF
	EStatusCode status;
	OutputFlateEncodeStream flateEncodeStream;
	OutputFlateDecodeStream flateDecodeStream;

	do
	{
		PDFWriter pdfWriter;
		OutputFile compressedLogFile;
		OutputStringBufferStream pdfStream;
	
		// setup log file with compression
		status = compressedLogFile.OpenFile("c:\\PDFLibTests\\CustomLogEncrypted.txt");
		if(status != PDFHummus::eSuccess)
			break;
		flateEncodeStream.Assign(compressedLogFile.GetOutputStream());
		
		// generate PDF
		TRACE_LOG("Starting PDF File Writing");
		status = pdfWriter.StartPDFForStream(&pdfStream,ePDFVersion13,LogConfiguration(true,&flateEncodeStream));
		if(status != PDFHummus::eSuccess)
			break;
		TRACE_LOG("Now will add an empty page");
		PDFPage* page = new PDFPage();

		page->SetMediaBox(PDFRectangle(0,0,400,400));
		
		status = pdfWriter.WritePageAndRelease(page);
		if(status != PDFHummus::eSuccess)
			break;

		TRACE_LOG("Added page, now will close");

		status = pdfWriter.EndPDFForStream();
		if(status != PDFHummus::eSuccess)
			break;

		// since log was started by starting PDF...the ending resets it. so let's now begin again
		Singleton<Trace>::GetInstance()->SetLogSettings(&flateEncodeStream,true);
		TRACE_LOG("Finished PDF!!!1");

		// dump PDF to a file, so we can review it
		OutputFile pdfFile;
		status = pdfFile.OpenFile("c:\\PDFLibTests\\DumpPDFFile.pdf");
		if(status != PDFHummus::eSuccess)
			break;

		string pdfString = pdfStream.ToString();
		pdfFile.GetOutputStream()->Write((const Byte*)pdfString.c_str(),pdfString.size());
		pdfFile.CloseFile();

		TRACE_LOG("PDF stream dumped");
		
		// now finalize trace compressed file
		flateEncodeStream.Assign(NULL);
		compressedLogFile.CloseFile();

		// Finish log
		Singleton<Trace>::Reset();


		// now open a new file and decompress the log into it.
		OutputFile decryptedLogFile;

		status = decryptedLogFile.OpenFile("c:\\PDFLibTests\\CustomLogDecrypted.txt");
		if(status != PDFHummus::eSuccess)
			break;


		// place an initial bom (cause the compressed content is unicode)
		unsigned short bom = (0xFE<<8) + 0xFF;
		decryptedLogFile.GetOutputStream()->Write((const Byte*)&bom,2);	

		flateDecodeStream.Assign(decryptedLogFile.GetOutputStream());
		OutputStreamTraits traits(&flateDecodeStream);

		InputFile compressedLogFileInput;
		status = compressedLogFileInput.OpenFile("c:\\PDFLibTests\\CustomLogEncrypted.txt");
		if(status != PDFHummus::eSuccess)
			break;

		status = traits.CopyToOutputStream(compressedLogFileInput.GetInputStream());
		if(status != PDFHummus::eSuccess)
			break;

		compressedLogFileInput.CloseFile();
		flateDecodeStream.Assign(NULL);
		decryptedLogFile.CloseFile();
		
	}while(false);

	if(status != PDFHummus::eSuccess)
	{
		// cancel ownership of subsstreams
		flateDecodeStream.Assign(NULL);
		flateEncodeStream.Assign(NULL);
	}

	return status;
}