void PadBMP( BMP& Input , int NewWidth , int NewHeight ) { // if the NewWidth and NewHight are unchanged, exit if( NewWidth == Input.TellWidth() && NewHeight == Input.TellHeight() ) { return; } // find max range for the copy, so that cropping occurs // if necessary int MaxWidth = Input.TellWidth(); if( MaxWidth > NewWidth ) { MaxWidth = NewWidth; } int MaxHeight = Input.TellHeight(); if( MaxHeight > NewHeight ) { MaxHeight = NewHeight; } // create a temporary image to hold the original pixels BMP Temp; Temp.SetSize(NewWidth,NewHeight); Temp.SetBitDepth( Input.TellBitDepth() ); int i,j; int Difference = Temp.TellHeight() - MaxHeight; for( i=0 ; i < MaxWidth ; i++ ) { for( j=0 ; j < MaxHeight ; j++ ) { *Temp(i,j+Difference) = *Input(i,j); } } // resize the original image, and recopy the pixels Input.SetSize(NewWidth,NewHeight); for( i=0 ; i < Input.TellWidth() ; i++ ) { for( j=0 ; j < Input.TellHeight() ; j++ ) { *Input(i,j) = *Temp(i,j); } } Temp.SetSize(1,1); Temp.SetBitDepth(24); return; }
// creates a BMP from a CImage // assumes all float values have already been scaled to [0.0, 255.0] BMP * CImage::toBmp() { BMP * bmp = new BMP(); bmp->SetSize(m_width, m_height); // for smaller output file size bmp->SetBitDepth(8); // 8-bit bitmaps need a color table CreateGrayscaleColorTable(*bmp); for (int col = 0; col < m_width; col++) { for (int row = 0; row < m_height; row++) { // Output is grayscale, so R, G, and B components are equal. // ScaleAndDisplayDisparityValues() et. al. in stereo.cpp have already // scaled all float pixel values to [0, 255]. ebmpBYTE byteVal = (ebmpBYTE) getValue(row, col, 0); (* bmp)(col, row)->Red = byteVal; (* bmp)(col, row)->Green = byteVal; (* bmp)(col, row)->Blue = byteVal; } } return bmp; }
//数组转图像 void array2bmp() { int i,j; BMP bmp; RGBApixel pix_black={0};//R=0 G=0 B=0为黑色 RGBApixel pix_white={255,255,255,0};//白色 bmp.SetSize(3,3); bmp.SetBitDepth(1); for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(array[i][j]==1) { bmp.SetPixel( i, j,pix_black); } else { bmp.SetPixel( i, j,pix_white); } } } bmp.WriteToFile("examp_array2bmp.bmp"); printf("array2bmp suc...\n"); }
void Camera::raytrace(Node *node, const std::vector<Light*>& lights) { BMP output; output.SetSize(width,height); output.SetBitDepth(24); for(int i = 0; i < width; i++) { for(int j = 0; j < height; j++) { output(i,j)->Red = 0; output(i,j)->Green = 0; output(i,j)->Blue = 0; } } float aRatio = (float)width/height; glm::vec3 xAxis = glm::cross(fwd, up); glm::vec3 yAxis = glm::cross(xAxis, fwd); float xFov = std::atan(std::tan(yFov) * aRatio); xAxis *= std::tan(xFov/2) * glm::length(fwd) / glm::length(xAxis); yAxis *= std::tan(yFov/2) * glm::length(fwd) / glm::length(yAxis); for(unsigned int j = 0; j < height; j++) { std::cout << "\rline " << j << std::flush; #pragma omp parallel for for(unsigned int i = 0; i < width; i++) { // indirect illumination glm::vec3 color(0); for(int d = 0; d < density; d++) { float x = (float(i) + float(rand())/RAND_MAX) / width; float y = (float(j) + float(rand())/RAND_MAX) / height; std::cout << "dbg " << fwd << " " << xAxis << " " << yAxis << "\n"; glm::vec3 dir = glm::normalize(fwd + (2*x-1)*xAxis + (1-2*y)*yAxis); Ray ray(pos, dir, rayCount, true); glm::vec4 dirCol = rayIter(ray, node, lights); glm::vec3 mcCol; if(dirCol[3] < 1 && mcIter > 0) { for(int w = 0; w < mcIter; w++) mcCol += rayIter(ray,node); mcCol /= float(mcIter); } else { dirCol[3] = 1; } color += (1.f - dirCol[3])*mcCol + dirCol[3]*glm::vec3(dirCol); } color /= float(density); output(i,j)->Red = 255.0*glm::clamp(color[0],0.f,1.f); output(i,j)->Green = 255.0*glm::clamp(color[1],0.f,1.f); output(i,j)->Blue = 255.0*glm::clamp(color[2],0.f,1.f); } } output.WriteToFile(outFile.c_str()); exit(0); }
bool Image::writeToBMPFile(const std::string & outputFileName) { bool success = true; if( m_pixels != NULL ) { // create bitmap image BMP outputImage; outputImage.SetSize(m_numCols, m_numRows); outputImage.SetBitDepth( 24 ); double maxVal = m_pixels[0]; double minVal = m_pixels[0]; // Maximum and minimum values for( int i = 1; i < m_numRows * m_numCols; ++i ) { if( m_pixels[i] > maxVal ) { maxVal = m_pixels[i]; } if( m_pixels[i] <= minVal ) { minVal = m_pixels[i]; } } for( int r = 0; r < m_numRows; ++r ) { for( int c = 0; c < m_numCols; ++c ) { // get pixel value and clamp between 0 and 255 double val = 255.0 * (m_pixels[r * m_numCols + c] - minVal) / (maxVal - minVal); if( val < 0 ) { val = 0; } if( val > 255 ) { val = 255; } // set output color based on mapping RGBApixel pixelVal; pixelVal.Blue = (int)val; pixelVal.Green = (int)val; pixelVal.Red = (int)val; outputImage.SetPixel(c, r, pixelVal); } } // write to file success = outputImage.WriteToFile( outputFileName.c_str() ); } else { success = false; } return success; }
bool HBITMAPtoBMP(HDC hDC,HBITMAP hBitmap,BMP& OutputImage) { using namespace std; bool output = false; BITMAPINFO BitInfo; ZeroMemory(&BitInfo, sizeof(BITMAPINFO)); BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // get all manner of information from the incoming bitmap if(!::GetDIBits(hDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS)) { return false; } // Set the size and bit depth of the BMP object OutputImage.SetSize( BitInfo.bmiHeader.biWidth ,BitInfo.bmiHeader.biHeight ); OutputImage.SetBitDepth(32); // reconfigure BitInfo.bmiHeader such that the resulting bitmap will be // 32 bits per pixel. This is _MUCH_ simpler BitInfo.bmiHeader.biBitCount = 32; BitInfo.bmiHeader.biCompression = 0; BitInfo.bmiHeader.biSizeImage = OutputImage.TellHeight()*OutputImage.TellWidth()*4; // I don't understand the +5 part here. -- Paul // BYTE *pData = new BYTE[BitInfo.bmiHeader.biSizeImage + 5]; // BYTE *pData = new BYTE[Output.TellHeight()*Output.TellWidth()*4+5]; BYTE *pData = new BYTE [BitInfo.bmiHeader.biSizeImage]; // now get the actual data if(!::GetDIBits(hDC, hBitmap, 0, BitInfo.bmiHeader.biHeight, pData, &BitInfo, DIB_RGB_COLORS)) { return false; } // transfer that data into the BMP object int i,j; int k=0; for( j=OutputImage.TellHeight()-1 ; j >= 0 ; j-- ) { for( i=0; i < OutputImage.TellWidth() ; i++ ) { OutputImage(i,j)->Blue = *(pData+k); k++; OutputImage(i,j)->Green = *(pData+k); k++; OutputImage(i,j)->Red = *(pData+k); k++; OutputImage(i,j)->Alpha = *(pData+k); k++; } } delete (pData); return true; }
int main( int argc, char *argv[] ) { cout << endl << "Using EasyBMP Version " << _EasyBMP_Version_ << endl << endl << "Copyright (c) by the EasyBMP Project 2005-6" << endl << "WWW: http://easybmp.sourceforge.net" << endl << endl; BMP Text; Text.ReadFromFile("EasyBMPtext.bmp"); BMP Background; Background.ReadFromFile("EasyBMPbackground.bmp"); BMP Output; Output.SetSize( Background.TellWidth() , Background.TellHeight() ); Output.SetBitDepth( 24 ); RangedPixelToPixelCopy( Background, 0, Output.TellWidth() - 1, Output.TellHeight() - 1 , 0, Output, 0, 0 ); RangedPixelToPixelCopyTransparent( Text, 0, 380, 43, 0, Output, 110, 5, *Text(0, 0) ); RangedPixelToPixelCopyTransparent( Text, 0, Text.TellWidth() - 1, Text.TellWidth() - 1, 50, Output, 100, 442, *Text(0, 49) ); Output.SetBitDepth( 32 ); cout << "writing 32bpp ... " << endl; Output.WriteToFile( "EasyBMPoutput32bpp.bmp" ); Output.SetBitDepth( 24 ); cout << "writing 24bpp ... " << endl; Output.WriteToFile( "EasyBMPoutput24bpp.bmp" ); Output.SetBitDepth( 8 ); cout << "writing 8bpp ... " << endl; Output.WriteToFile( "EasyBMPoutput8bpp.bmp" ); Output.SetBitDepth( 4 ); cout << "writing 4bpp ... " << endl; Output.WriteToFile( "EasyBMPoutput4bpp.bmp" ); Output.SetBitDepth( 1 ); cout << "writing 1bpp ... " << endl; Output.WriteToFile( "EasyBMPoutput1bpp.bmp" ); Output.SetBitDepth( 24 ); Rescale( Output, 'p' , 50 ); cout << "writing 24bpp scaled image ..." << endl; Output.WriteToFile( "EasyBMPoutput24bpp_rescaled.bmp" ); return 0; }
// same as above, but highlights a range of depths in red, // and, colors all depths outside that range black in refImage // highlight_low and high are values in [1,100] BMP * CImage::toHighlightedBmp(int highlight_low, int highlight_high, BMP * refBmp) { // scale range to be highlighted to [0,255] highlight_low = ((float) highlight_low) * 255.0/100.0 - 2.55; highlight_high = ((float) highlight_high) * 255.0/100.0; BMP * bmp = new BMP(); bmp->SetSize(m_width, m_height); // for smaller output file size bmp->SetBitDepth(8); // 8-bit bitmaps need a color table CreateGrayscaleColorTable(*bmp); // add red to the color table RGBApixel highlightColor; highlightColor.Red = 255; highlightColor.Green = 0; highlightColor.Blue = 0; highlightColor.Alpha = 0; bmp->SetColor(highlight_low, highlightColor); // copy pixels to bmp for (int col = 0; col < m_width; col++) { for (int row = 0; row < m_height; row++) { float pixel = getValue(row, col, 0); if (highlight_low <= pixel && pixel <= highlight_high) { (* bmp)(col, row)->Red = 255; (* bmp)(col, row)->Green = 0; (* bmp)(col, row)->Blue = 0; } else { // Output is grayscale, so R, G, and B components are equal. // ScaleAndDisplayDisparityValues() et. al. in stereo.cpp have already // scaled all float pixel values to [0, 255]. ebmpBYTE byteVal = (ebmpBYTE) pixel; (* bmp)(col, row)->Red = byteVal; (* bmp)(col, row)->Green = byteVal; (* bmp)(col, row)->Blue = byteVal; // color non-highlighted areas black in refBmp (* refBmp)(col, row)->Red = 0; (* refBmp)(col, row)->Green = 0; (* refBmp)(col, row)->Blue = 0; } } } return bmp; }
void w2f(GlobalMap &globalMap, int i, int j, int size=1025) { char fileName[6] = {'0','0','.','b','m','p'}; fileName[0]=i+48; fileName[1]=j+48; short z; RGBApixel color; BMP image; image.SetSize(size,size); image.SetBitDepth(16); for(int y=0; y<size; y++) { for(int x=0; x<size; x++) { z= globalMap.getLocalMap(i,j)->getHeight(x,y); if(z<0) { color.Red=0; color.Green=0; color.Blue=(z+32768)/129; color.Alpha=0; } if(z>=0 && z<20000) { color.Red=0; color.Green=z/134+100; color.Blue=0; color.Alpha=0; } if(z>=20000 && z<25000) { color.Red=(z-20000)/500+200; color.Green=(z-20000)/500+200; color.Blue=(z-20000)/500+200; color.Alpha=0; } if(z>=25000) { color.Red=255; color.Green=255; color.Blue=255; color.Alpha=0; } image.SetPixel(x,y, color); } } image.WriteToFile(fileName); }
void BitmapRawConverter::pixelsToBitmap(char *outFilename) { BMP out; out.SetSize(width, height); out.SetBitDepth(24); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { out.SetPixel(i, j, getPixel(i,j)); } } out.WriteToFile(outFilename); }
int main(int argc, char** argv) { if (argc != 2) { cout << "Incorrect input. Please enter the name of the configuration file." << endl; return -1; } Reader * config = new Reader(argv[1]); int width = config->getWIDTH(); int height = config->getHEIGHT(); int x = config->getX(); int y = config->getY(); int z = config->getZ(); Camera * rayGenerator = new Camera(config->getEYEP(), config->getVDIR(), config->getUVEC(), config->getFOVY(), width, height); BMP output; output.SetSize(width, height); output.SetBitDepth(24); Raymarch * raymarch = new Raymarch(config->getVB(), rayGenerator, config->getEYEP(), config->getMRGB(), config->getBRGB(), config->getLCOL(), config->getLPOS(), config->getOFST()); for (int w = 0; w < width; w++) { cout << "Rendering: " << (float) w/width << endl; for (int h = 0; h < height; h++) { /* // Ray direction color test output(w,h)->Red = abs(rayGenerator->getRayDirection(w,h).x) *255; output(w,h)->Green = abs(rayGenerator->getRayDirection(w,h).y) *255; output(w,h)->Blue = abs(rayGenerator->getRayDirection(w,h).z) *255; */ glm::vec3 colors = raymarch->getColor(w,h, config->getSTEP(), glm::vec3(x, y, z)); colors*=255.0; if (colors.x > 255) colors.x = 255; if (colors.y > 255) colors.y = 255; if (colors.z > 255) colors.z = 255; output(w,h)->Red =colors.x; output(w,h)->Green = colors.y; output(w,h)->Blue = colors.z; } } output.WriteToFile(config->getFILE()); cout << "File has been finished rendering." <<endl; return 0; }
void Camera::printImage(char* outputName) { BMP output; output.SetSize(imgWidth, imgHeight); output.SetBitDepth(24); glm::vec3 outR; for (int i = 0; i < imgWidth; i++) { for (int j = 0; j < imgHeight; j++) { outR = glm::abs(glm::normalize(getDirectionFromCoordinate(i,j)-eye)); output(i,j)->Red = outR.x * 255; output(i,j)->Green = outR.y * 255; output(i,j)->Blue = outR.z * 255; } } output.WriteToFile(outputName); }
static cell AMX_NATIVE_CALL n_FSetPixelRGBA( AMX* amx, cell* params ){ posx = params[1]; posy = params[2]; RGBApixel value; value.Red = (ebmpBYTE)params[4]; value.Green = (ebmpBYTE)params[5]; value.Blue = (ebmpBYTE)params[6]; value.Alpha = (ebmpBYTE)params[7]; GlobalImage.SetBitDepth(24); GlobalImage.GetPixel(posx,posy); GlobalImage.SetPixel(posx,posy,value); return 1; }
bool ExportBMP( const _TImg_t & in_indexed, const std::string & filepath ) { //shorten this static constant typedef utils::do_exponent_of_2_<_TImg_t::pixel_t::mypixeltrait_t::BITS_PER_PIXEL> NbColorsPP_t; BMP output; output.SetBitDepth(_TImg_t::pixel_t::GetBitsPerPixel()); if( in_indexed.getNbColors() != NbColorsPP_t::value ) { #ifdef _DEBUG assert(false); #endif throw std::runtime_error( "ERROR: The tiled image to write to a bitmap image file has an invalid amount of color in its palette!" ); } //copy palette for( int i = 0; i < NbColorsPP_t::value; ++i ) output.SetColor( i, colorRGB24ToRGBApixel( in_indexed.getPalette()[i] ) ); //Copy image output.SetSize( in_indexed.getNbPixelWidth(), in_indexed.getNbPixelHeight() ); for( int i = 0; i < output.TellWidth(); ++i ) { for( int j = 0; j < output.TellHeight(); ++j ) { auto & refpixel = in_indexed.getPixel( i, j ); uint8_t temp = static_cast<uint8_t>( refpixel.getWholePixelData() ); output.SetPixel( i,j, colorRGB24ToRGBApixel( in_indexed.getPalette()[temp] ) ); //We need to input the color directly thnaks to EasyBMP } } bool bsuccess = false; try { bsuccess = output.WriteToFile( filepath.c_str() ); } catch( std::exception e ) { cerr << "<!>- Error outputing image : " << filepath <<"\n" << " Exception details : \n" << " " <<e.what() <<"\n"; assert(false); bsuccess = false; } return bsuccess; }
bool _surf_save_bmp(const d_surf * srf, const char * filename) { if (!filename) return false; if (!srf) { writelog(LOG_ERROR,"surf_save_png : no surf loaded"); return false; } writelog(LOG_MESSAGE,"Saving surf %s to file %s (BMP)", srf->getName(), filename); size_t NN = srf->getCountX(); size_t MM = srf->getCountY(); BMP res; res.SetSize(NN,MM); res.SetBitDepth(24); REAL minz, maxz; srf->getMinMaxZ(minz, maxz); size_t i, j; double gray_color; for (j = 0; j < MM; j++) { for (i = 0; i < NN; i++) { REAL value = srf->getValueIJ(i,MM-j-1); if (value == srf->undef_value) { res(i,j)->Red = 0; res(i,j)->Green = 0; res(i,j)->Blue = 0; res(i,j)->Alpha = 0; } else { gray_color = MAX(0,MIN(255,floor((value - minz)/(maxz-minz)*255 + 0.5))); res(i,j)->Red = (ebmpBYTE)gray_color; res(i,j)->Green = (ebmpBYTE)gray_color; res(i,j)->Blue = (ebmpBYTE)gray_color; res(i,j)->Alpha = 255; } } } res.WriteToFile(filename); return true; };
static cell AMX_NATIVE_CALL n_SetPixelRGBA( AMX* amx, cell* params ){ amx_StrParam(amx,params[1],tmp); BMP Image; posx = params[2]; posy = params[3]; Image.ReadFromFile(tmp); RGBApixel value; value.Red = (ebmpBYTE)params[4]; value.Green = (ebmpBYTE)params[5]; value.Blue = (ebmpBYTE)params[6]; value.Alpha = (ebmpBYTE)params[7]; Image.SetBitDepth(24); Image.GetPixel(posx,posy); Image.SetPixel(posx,posy,value); return Image.WriteToFile(tmp); }
void Camera::debugRender(unsigned int width, unsigned int height, std::string& outputFile) { BMP file; file.SetSize(width,height); file.SetBitDepth(24); double d = m_direction.length(); //Distance from Eye to Middle point of the image Vector myVectorEyetoMiddle(m_direction); myVectorEyetoMiddle *= d; //C in full length double tanfovx = tan(m_fovRad)*double(width)/height ; Vector myEyeVectorX = myVectorEyetoMiddle.crossProduct(m_up) ; //A Vector myEyeVectorY = myEyeVectorX.crossProduct(myVectorEyetoMiddle) ; //B Vector myMiddlePoint = myVectorEyetoMiddle + m_position; Vector myImageVectorX = myEyeVectorX * ( myVectorEyetoMiddle.normalize().length() * tanfovx / myEyeVectorX.length() ); //H Vector myImageVectorY = myEyeVectorY * ( myVectorEyetoMiddle.normalize().length() * tan(m_fovRad) / myEyeVectorY.length() ); //V for(unsigned int i=0;i<width;i++){ for(unsigned int j=0;j<height;j++){ double sx,sy; sx = double(i)/width; sy = double(j)/height; Vector P = myImageVectorX*(2*sx-1) + myImageVectorY*(2*sy-1) + myMiddlePoint ; Vector myRayCasting = (P - m_position)*(1.0 / (P - m_position).normalize().length()); //RayDirection = (P - E)/normalize(P - E); Vector normalizedRay = myRayCasting.normalize(); RGBApixel NewColor; NewColor.Red = (ebmpBYTE) abs(normalizedRay[0])*255; NewColor.Green = (ebmpBYTE) abs(normalizedRay[1])*255; NewColor.Blue = (ebmpBYTE) abs(normalizedRay[2])*255; NewColor.Alpha = 0; file.SetPixel(i, height-j-1, NewColor); } } file.WriteToFile(outputFile.c_str()); }
void DisplayClass::doRayTrace() { graph->rootNode->computeAllInverses(); unsigned int width = static_cast<int>(*resoX); unsigned int height = static_cast<int>(*resoY); glm::vec3 A = glm::cross(rayCamera->center, rayCamera->up); glm::vec3 B = glm::cross(A, rayCamera->center); glm::vec3 M = rayCamera->center + rayCamera->eye; glm::vec3 V = (B * glm::length(rayCamera->center) * tan(glm::radians(rayCamera->fovy)))/ glm::length(B); //float fovx = atan(length(V) * (*WriteBMP::resoX/ *WriteBMP::resoY)/length(M)); glm::vec3 H = (*resoX/ *resoY) * V; H.x = H.y; H.y = 0.0f; H.z = 0.0f; BMP output; output.SetSize(width, height); output.SetBitDepth(24); glm::vec3 P; glm::vec3 D; glm::vec3* E = new glm::vec3(rayCamera->eye.x, rayCamera->eye.y, rayCamera->eye.z); glm::vec3* color = new glm::vec3(); std::cout << "Beginning raytrace" << std::endl; for(unsigned int x = 0; x < width; x++) { for(unsigned int y = 0; y < height; y++) { color = new glm::vec3(0.0f, 0.0f, 0.0f); E->x = rayCamera->eye.x; E->y = rayCamera->eye.y; E->z = rayCamera->eye.z; P = DisplayClass::mapPoint(x, height - y - 1, M, H, V); D = glm::normalize(P - *E);///glm::length(P - *E); traceRay(color, 0, *E, D, 1.0f, 1.0f, *rayLightCol->at(0)); output(x, y)->Red = 255 * color->x; output(x, y)->Green = 255 * color->y; output(x, y)->Blue = 255 * color->z; delete color; color = 0; } if (x % 10 == 0) { std::cout << "finished vertical line: " << x << std::endl; } } std::cout << "Finished raytrace!" << std::endl; output.WriteToFile(rayOutputFile->c_str()); }
void raycast(mat4 cameraMatrix) { int width = 800; int height = 600; const float Wd2 = width/2; const float Hd2 = height/2; BMP output; output.SetSize(width, height); output.SetBitDepth(24); glm::vec4 camerapos = glm::vec4(0, 0, 0, 0); for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { float xtemp = x - Wd2; float ytemp = y - Hd2; xtemp = xtemp/Wd2; ytemp = ytemp/Hd2; glm::vec4 pixelpos = glm::vec4(xtemp,ytemp,1,0); glm::vec4 ray = camerapos-pixelpos; ray = glm::normalize(ray); vec4 color = intersectTest(ray); output(x,y)->Red = color.r; output(x,y)->Green = color.g; output(x,y)->Blue = color.b; } } output.WriteToFile("raytrace.bmp"); return; }
int main(int argc, char* argv[]) { cout << "Enter filename with max 30 characters:" << endl; char s[30]; cin.getline(s, 30); //char* s = std::cin; BMP Image; if (Image.ReadFromFile(s)){ int height = Image.TellHeight(); int width = Image.TellWidth(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { double Temp = 0.30*(Image(x, y)->Red) + 0.59*(Image(x, y)->Green) + 0.11*(Image(x, y)->Blue); Image(x, y)->Red = (Byte)Temp; Image(x, y)->Green = (Byte)Temp; Image(x, y)->Blue = (Byte)Temp; } } Image.SetBitDepth(8); CreateGrayscaleColorTable(Image); char output[30] = "grey_"; strcat(output, s); std::cout << output << std::endl; Image.WriteToFile(output); } std::cin.get(); return 0; }
//******************* // outputs the image to a bmp // by writing the value of the array we have // to each of RG, and B //******************* void outputImg(void){ //setup Output IMG BMP OutputIMG; int byte; OutputIMG.SetBitDepth(DEPTH); OutputIMG.SetSize(COLUMNS,ROWS); cout<< "Output Image to File" << endl; for( int j=0 ; j < ROWS ; j++) { for( int i=0 ; i < COLUMNS ; i++) { byte = imageArray[i][j]; OutputIMG(i,j)->Red = byte; OutputIMG(i,j)->Green = byte; OutputIMG(i,j)->Blue = byte; } } OutputIMG.WriteToFile("Output.bmp"); cout << "\n**** NOW GO OPEN Output.BMP ******" << endl; }
int main( int argc, char* argv[] ) { const int width = 3, thresh = 5; BMFH bmfh = GetBMFH(argv[1]); if (bmfh.bfType != BMP::BMP_FILE_TYPE) { return -1; } BMP inImage; inImage.ReadFromFile(argv[1]); BMP outImage; outImage.SetSize(inImage.TellWidth(), inImage.TellHeight()); outImage.SetBitDepth(24); const int numNbr = (2 * width + 1) * (2 * width + 1) - 1; for( int i=width ; i < inImage.TellWidth()-width ; i++) { for( int j=width ; j < inImage.TellHeight()-width ; j++) { // find average of neighboring input pixels int rPij = 0, gPij = 0, bPij = 0; for (int di = -width; di <= width; ++di) { for (int dj = -width; dj <= width; ++dj) { if (di == 0 && dj == 0) continue; rPij += inImage.redPixel(i+di, j+dj); gPij += inImage.greenPixel(i+di, j+dj); bPij += inImage.bluePixel(i+di, j+dj); } } outImage.redPixel(i,j) = filter(inImage.redPixel(i,j), rPij / numNbr, thresh); outImage.greenPixel(i,j) = filter(inImage.greenPixel(i,j), gPij / numNbr, thresh); outImage.bluePixel(i,j) = filter(inImage.bluePixel(i,j), bPij / numNbr, thresh); } } outImage.WriteToFile(argv[2]); return 0; }
int main() { long start = getMsTime(); //state = getState(); // printf("Original camera: (%f, %f, %f), %p\n", state->camera.x, state->camera.y, state->camera.z, state); PpuPThreadData datas[MAX_SPE_THREADS]; int spe_threads = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1); int count; if (spe_threads > MAX_SPE_THREADS) spe_threads = MAX_SPE_THREADS; Scene* scene = getScene(); int numberOfColorGroups = (int)round((X_PIXELS*Y_PIXELS)/4.0); int size = sizeof(ColorGroup)*numberOfColorGroups; ColorGroup* pixels = (ColorGroup*)aligned_alloc(size, 10); BMP myImage; myImage.SetSize(X_PIXELS, Y_PIXELS); myImage.SetBitDepth(24); int xPixel_4 = X_PIXELS / 4; VectorSInt rInt; VectorSInt gInt; VectorSInt bInt; long stateTime = getMsTime() - start; long spuCreationTime = getMsTime(); for (int i = 0, offset = 0; i < spe_threads; i++, offset += count) { /* Create a SPE context */ if ((datas[i].spe_ctx = spe_context_create (0, NULL)) == NULL) { perror ("Failed creating context"); exit (1); } /* Load SPE program into the SPE context*/ if (spe_program_load (datas[i].spe_ctx, &raytracer_spe)) { perror ("Failed loading program"); exit (1); } /* Initialize context run data */ RayTraceState* state = getState(pixels + offset, scene); count = (numberOfColorGroups / spe_threads + 1023) & ~1023; state->offset = offset; state->pixelsToProcess = (i == spe_threads - 1) ? numberOfColorGroups - offset : count; datas[i].argp = state; //printf("Pixels: %p, ncg: %d, count: %d, offset: %d\n", pixels + offset, numberOfColorGroups, count, offset); /* Create pthread for each of the SPE contexts */ if (pthread_create (&datas[i].pthread, NULL, &ppu_pthread_function, &datas[i])) { perror ("Failed creating thread"); exit (1); } } spuCreationTime = getMsTime() - spuCreationTime; long processingTime = 0; long totalSpuTime = 0; for (int spe = 0; spe < spe_threads; spe++) { long spuTime = getMsTime(); /* Wait for the threads to complete */ if (pthread_join (datas[spe].pthread, NULL)) { perror ("Failed joining thread\n"); exit (1); } if (spe_context_destroy(datas[spe].spe_ctx) != 0) { perror("Failed destroying context"); exit (1); } spuTime = getMsTime() - spuTime; totalSpuTime += spuTime; printf("Time waiting for spu %d: %li\n", spe, spuTime); long tidbitProcessingTime = getMsTime(); RayTraceState* state = (RayTraceState*)datas[spe].argp; int offset = state->offset; int row = offset / xPixel_4; int column = offset % xPixel_4; int pixelsToProcess = state->pixelsToProcess; int pixelsProcessed = 0; bool breakOut = false; printf("Now outputting pixels for row %d, column %d\n", row, column); int i = 0, j = 0; for (i = row; i < Y_PIXELS; i++) { for (j = column; j < xPixel_4; j++) { int index = i * xPixel_4 + j; rInt.vec = vec_cts(pixels[index].r, 8); gInt.vec = vec_cts(pixels[index].g, 8); bInt.vec = vec_cts(pixels[index].b, 8); for (int k = 0; k < 4; k++) { myImage(4*j+k,i)->Red = round(rInt.points[k]); myImage(4*j+k,i)->Green = round(gInt.points[k]); myImage(4*j+k,i)->Blue = round(bInt.points[k]); } if (++pixelsProcessed == pixelsToProcess) { breakOut = true; break; } } column = 0; if (breakOut) { break; } } tidbitProcessingTime = getMsTime() - tidbitProcessingTime; processingTime += tidbitProcessingTime; printf("Processing %d pixels (offset: %d, up to row %d, column %d) for spu %d: %li\n", pixelsToProcess, offset, i, 4*j + 3, spe, tidbitProcessingTime); } long fileWritingTime = getMsTime(); myImage.WriteToFile(FILE_NAME); fileWritingTime = getMsTime() - fileWritingTime; long time = getMsTime() - start; printf("Finished. Total time: %li (state: %li, spu creation: %li, spu execution: %li, processing: %li, file: %li)\n", time, stateTime, spuCreationTime, totalSpuTime, processingTime, fileWritingTime); return(0); }
int hpx_main() { BMP SetImage; SetImage.SetBitDepth(24); SetImage.SetSize(sizeX * 2, sizeY); hpx::util::high_resolution_timer t; { using namespace std; using hpx::future; using hpx::async; using hpx::wait_all; int const max_iteration = 255; vector<future<int> > iteration; iteration.reserve(sizeX*sizeY); hpx::cout << "Initial setup completed in " << t.elapsed() << "s. Initializing and running futures...\n" << hpx::flush; t.restart(); { hpx::threads::executors::local_queue_executor exec; for (int i = 0; i < sizeX; ++i) { for (int j = 0; j < sizeY; ++j) { float x0 = (float)i * 3.5f / (float)sizeX - 2.5f; float y0 = (float)j * 2.0f / (float)sizeY - 1.0f; iteration.push_back(async(exec, &fractal_pixel_value, x0, y0, max_iteration)); } } // the executor's destructor will wait for all spawned tasks to // finish executing } hpx::cout << sizeX*sizeY << " calculations run in " << t.elapsed() << "s. Transferring from futures to general memory...\n" << hpx::flush; t.restart(); for (int i = 0; i < sizeX; ++i) { for (int j = 0; j < sizeY; ++j) { int it = iteration[i*sizeX + j].get(); for (int k = 0; k < 2; ++k) { int p = (it * 255) / max_iteration; SetImage.SetPixel(i * 2 + k, j, RGBApixel(p, p, p)); } } } } hpx::cout << "Transfer process completed in " << t.elapsed() << "s. Writing to hard disk...\n" << hpx::flush; t.restart(); SetImage.WriteToFile("out.bmp"); hpx::cout << "Fractal image written to file \"out.bmp\" from memory in " << t.elapsed() << "s.\nShutting down process.\n" << hpx::flush; return hpx::finalize(); // Handles HPX shutdown }
int main(int argc, char* argv[]) { int width = 161, height = 161, layer = 80; int i, j; nifti_image *nim = NULL; char *fin = "/Users/jia/Desktop/F1_58-ed.nii"; nim = nifti_image_read(fin, 1); if (!nim) { fprintf(stderr,"** failed to read NIfTI image from '%s'\n", fin); return 2; } printf("%dD, x=%d y=%d z=%d t=%d u=%d v=%d w=%d\n", nim->ndim, nim->nx, nim->ny, nim->nz, nim->nt, nim->nu, nim->nv, nim->nw); printf("nvox=%d nbyper=%d\n", nim->nvox, nim->nbyper); printf("%fD x=%f y=%f z=%f t=%f u=%f v=%f w=%f\n", nim->pixdim[0], nim->dx, nim->dy, nim->dz, nim->dt, nim->du, nim->dv, nim->dw); unsigned short *q = static_cast<unsigned short *>(nim->data); unsigned short max = 0, min = 0, mean = 0; unsigned long long sum = 0; for (int i = width*height*layer; i < width*height*(layer+1); i++) { sum += q[i]; // printf("%u\n", q[i]); if (q[i] > max) max = q[i]; } mean = sum / (width*height); printf("max=%u min=%u mean=%u\n", max, min, mean); //=================================================================== // suv_max_group = max(temp_img)*weight/(decay_factor*dose); // suv_mean_group = mean(temp_img)*weight/(decay_factor*dose); //weight INPUT //dose INPUT //time INPUT //half_life INPUT //decay_factor = 2^(-time*60/half_life); //=================================================================== int weight = 156; int dose = 156; int time = 16; int half_life = 15; int decay_factor = 2^(-time*60/half_life); printf("weight=%d decay_factor=%d dose=%d\n", weight, decay_factor, dose); int suv_max = max * weight / (decay_factor * dose); int suv_mean = mean * weight / (decay_factor * dose); printf("suv_max=%d suv_mean=%d\n", suv_max, suv_mean); unsigned short p[width*height]; for (int i = 0; i < width*height; i++) { p[i] = q[width*height*layer + i] * 255 / max; } BMP Output; Output.SetSize(width, height); Output.SetBitDepth(24); for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { RGBApixel NewPixel; if (check_area(i, j) == 1) { NewPixel.Red = p[i*width + j]; NewPixel.Green = p[i*width + j]; NewPixel.Blue = p[i*width + j]; } else { NewPixel.Red = 0; NewPixel.Green = 0; NewPixel.Blue = 0; } NewPixel.Alpha = 0; Output.SetPixel(i, j, NewPixel); } } Output.SetBitDepth(32); Output.WriteToFile("EasyBMPoutput32bpp.bmp"); Output.SetBitDepth(24); Output.WriteToFile("EasyBMPoutput24bpp.bmp"); Output.SetBitDepth(8); Output.WriteToFile("EasyBMPoutput8bpp.bmp"); Output.SetBitDepth(4); Output.WriteToFile("EasyBMPoutput4bpp.bmp"); Output.SetBitDepth(1); Output.WriteToFile("EasyBMPoutput1bpp.bmp"); Output.SetBitDepth(24); Rescale(Output, 'p', 50); Output.WriteToFile("EasyBMPoutput24bpp_rescaled.bmp"); /* and clean up memory */ nifti_image_free(nim); return 0; }
int main(int argc, char * argv[]) { BMP image; image.SetSize(W,H); image.SetBitDepth(32); std::vector<Camera> cameras; Scene scene; // check for proper usage if(argc == 1 || argc > 2) { fprintf(stderr, "%s: usage: %s FILENAME\n", argv[0], argv[0]); return 1; } Parser* fileParser = new Parser(); if(fileParser->parse(argv[1]) == 1) return 1; cameras = fileParser->getCameras(); scene = fileParser->getScene(); for(size_t i = 0; i < scene.planes.size(); i++) { Plane* curr_plane = &scene.planes.at(i); if(curr_plane->hastexture) { curr_plane->texture.ReadFromFile(curr_plane->textureFN.c_str()); } } // for each camera for(int i = 0; i < cameras.size(); i++) { Camera* current_camera = &cameras.at(i); // for every pixel for(int px=0; px<W; px++) { printf("printing column: %d\n", px); for(int py=0; py<H; py++) { //printf("coloring %d %d\n", px, py); Color current_pix = colorPixel(px, py, &scene, current_camera); image(px, py)->Red = (ebmpBYTE) 255 * current_pix.r; image(px, py)->Green = (ebmpBYTE) 255 * current_pix.g; image(px, py)->Blue = (ebmpBYTE) 255 * current_pix.b; image(px, py)->Alpha = 0.0; //printf("%d\t%d\n", px, py); //printf("%f\t%f\t%f\n", current_pix.r, current_pix.g, current_pix.b); } } std::string inputfileName = argv[1]; size_t pos = inputfileName.find_last_of('.'); inputfileName = inputfileName.substr(0, pos); char* camnum = new char[20]; std::string s; std::stringstream out; out << i; s = out.str(); std::string outfileName = inputfileName; outfileName.append("-"); outfileName.append(s); outfileName.append(".bmp"); image.WriteToFile(outfileName.c_str()); } //BMP texture; //texture.ReadFromFile("output.bmp"); free(fileParser); return 0; }
//trilinear interpolation simualtion void TrilinearInterpolationSimulation::Simulation() { //declare an object for ifstream ifstream myReadFile; //open the first txt file "test1.txt" myReadFile.open(str); //declare a index int index =0; //declare a vector to store the string vector<string> vSstore (1010000); //declare a vector to store Q vector<float> vQstore (1001000); //declare a temporary number float temp = 0; //declare a vector to store voxel; vector<voxel> vVstore (1010000); //read the contents from "text1.txt" if(myReadFile.is_open()) { string str; while(!myReadFile.eof()){ getline(myReadFile, str); vSstore[index] = str; index++; } } //store all the density into the vector for(int i = 0; i<1000000; ++i) { from_string<float>(temp, std::string(vSstore[i+13]), std::dec); vVstore[i].setDensity(temp); if(str == "test1.txt") { vVstore[i].setRedValue(245); vVstore[i].setBlueValue(245); vVstore[i].setGreenValue(245); } if(str == "test2.txt") { vVstore[i].setRedValue(240); vVstore[i].setGreenValue(247); vVstore[i].setBlueValue(255); } if(str == "test3.txt") { vVstore[i].setRedValue(33); vVstore[i].setGreenValue(140); vVstore[i].setBlueValue(33); } } myReadFile.close(); //define the step size const float step = 0.5; //set light Red corlor const int lightRedColor = 1; //set light blue color const int lightBlueColor = 1; //set light green color const int lightGreenColor = 1; //check which combination of background color we should use if(str == "test1.txt") { backRedColor = 69; backGreenColor = 130; backBlueColor = 181; kapa = (float) 0.7; } if(str == "test2.txt") { backRedColor = 255; backGreenColor = 69; backBlueColor = 0; kapa = (float)0.9; } if(str == "test3.txt") { backRedColor = 0; backGreenColor = 0; backBlueColor = 204; kapa = (float)0.4; } BMP output; output.SetSize(640, 480); output.SetBitDepth (24); //store the precomputed transmittance for(int i = -320; i< 320; ++i) { for(int j = -240; j< 240; ++j) { float a = (float) (200*200 + j*j + i*i); float length = sqrt(a); //calculate the corresponding step size in the x, y, z direction stepX = 200*step/length; stepY = i*step/length; stepZ = j*step/length; //calculate step times int stepTime = (int) (length/step); //delcare delta Q float deltaQ = 0.0; //declare Q float Q = 0.5; //declare dentisty float Density = 0.0; //delcare weight float weightX = 0.0; float weightY = 0.0; float weightZ = 0.0; float weight =0.0; //go through the light one by one for(int l= 1; l<= stepTime; ++l) { x = (float)(200-l*stepX); y = (float)(l*stepY); z = (float)(l*stepZ); float interpolatedDensity = 0.0; if((x>(0+xMove)&&x<(100+xMove))&&(y>(0+yMove)&&y<(100+yMove))&&(z>(0+zMove)&&z<(100+zMove))) { //convert x,y,z into integer voxelX = (int) (x-xMove); voxelY = (int) (y-yMove); voxelZ = (int) (z-zMove); for(int a = voxelX; a<= voxelX+1; ++a) { for(int b = voxelY; b<= voxelY+1; ++b) { for(int c = voxelZ; c<= voxelZ+1; ++c) { weightX = (1-abs(x-(float)(a+xMove)))/1; weightY = (1-abs(y-(float)(b+yMove)))/1; weightZ = (1-abs(z-(float)(c+zMove)))/1; weight = weightX*weightY*weightZ; index = a +100*b + c*100*100; if(index <= 1000000) { interpolatedDensity += vVstore[index].getDensity()*weight; } } } } index = voxelX + voxelY*100 + voxelZ*100*100; deltaQ = exp(-kapa*step*interpolatedDensity); Q *= deltaQ; vQstore[index]= Q; } } } } //ray match between eye and voxel for(int i = -320; i < 320; ++i) { for(int j= -240; j < 240; ++j) { //calculate and record the total length of the ray float a = (float) (i*i + j*j + 240*240); float length = sqrt(a); //the red color in the pixel int redColor = 0; //the green color in the pixel int greenColor = 0; //the blue color in the pixel int blueColor = 0; //calculate how many steps should go in the ray int stepTime = (int) (length/step); //density is used to record voxel density float Density = 0.0; //define transmittance; float Transmittance = 1.0; //deltaT is used to calculate transmittance float deltaT = 0.0; //calculate the corresponding step size in the x, y, z direction stepX = i*step/length; stepY = j*step/length; stepZ = 240*step/length; //declare weight float weightX = 0.0; float weightY = 0.0; float weightZ = 0.0; float weight =0.0; for(int k = 1; k <= stepTime; ++k) { x = (float)(k*stepX); y = (float)(k*stepY); z = (float)(200.0-k*stepZ); //conditional judgement to see if light is cast into the grid if((x>(0+xMove)&&x<(100+xMove))&&(y>(0+yMove)&&y<(100+yMove))&&(z>(0+zMove)&&z<(100+zMove))) { //convert x,y,z into integer voxelX = (int) (x-xMove); voxelY = (int) (y-yMove); voxelZ = (int) (z-zMove); float interpolatedDensity = 0.0; float interpolatedRedColor = 0.0; float interpolatedGreenColor =0.0; float interpolatedBlueColor =0.0; for(int a = voxelX; a<= voxelX+1; ++a) { for(int b = voxelY; b<= voxelY+1; ++b) { for(int c = voxelZ; c<= voxelZ+1; ++c) { weightX = (1-abs(x-(float)(a+xMove)))/1; weightY = (1-abs(y-(float)(b+yMove)))/1; weightZ = (1-abs(z-(float)(c+zMove)))/1; weight = weightX*weightY*weightZ; index = a + b*100 + c*100*100; if(index <= 1000000) { //interpolate density interpolatedDensity += vVstore[index].getDensity()*weight; //interpolate redColor interpolatedRedColor += vVstore[index].getRedValue()*weight; //interpolate greenColor interpolatedGreenColor += vVstore[index].getGreenValue()*weight; //interpolate blueColor interpolatedBlueColor += vVstore[index].getBlueValue()*weight; } } } } deltaT = exp(-kapa*step*interpolatedDensity); Transmittance*=deltaT; if(Transmittance < exp(-6.0)) { break; } index = voxelX + 100*voxelY + 100*100*voxelZ; redColor += (int)((1-deltaT)/kapa*(interpolatedRedColor*lightRedColor)*Transmittance*vQstore[index]); greenColor += (int)((1-deltaT)/kapa*(interpolatedGreenColor*lightGreenColor)*Transmittance*vQstore[index]); blueColor += (int)((1-deltaT)/kapa*(interpolatedBlueColor*lightBlueColor)*Transmittance*vQstore[index]); } } //set the color by adding material color with blackground color if(Transmittance > exp(-6.0)){ redColor = (int) (redColor*(1-Transmittance)+ backRedColor*Transmittance); greenColor = (int) (greenColor*(1-Transmittance) + backGreenColor*Transmittance); blueColor = (int) (blueColor*(1-Transmittance) + backBlueColor*Transmittance); } output(i+320, 239-j)->Red = redColor; output(i+320, 239-j)->Blue = blueColor; output(i+320, 239-j)->Green = greenColor; } } //check the file name if(str == "test1.txt") { output.WriteToFile("BabysFirstCloudImage.bmp"); }else if (str == "test2.txt") { output.WriteToFile("FadedCloud.bmp"); }if (str == "test3.txt") { output.WriteToFile("BothInOne.bmp"); } }
void Camera::renderRayBoxIntersection(unsigned int width, unsigned int height, float m_bgcolor[], float m_boxcolor[], std::string& outputFile){ BMP file; file.SetSize(width,height); file.SetBitDepth(24); Vector myVectorEyetoMiddle(m_direction.normalize()); double tanfovx = tan(m_fovRad)*double(width)/height ; Vector myEyeVectorX = myVectorEyetoMiddle.crossProduct(m_up) ; //A Vector myEyeVectorY = myEyeVectorX.crossProduct(myVectorEyetoMiddle) ; //B Vector myMiddlePoint = myVectorEyetoMiddle + m_position; Vector myImageVectorX = myEyeVectorX * ( myVectorEyetoMiddle.normalize().length() * tanfovx / myEyeVectorX.length() ); //H Vector myImageVectorY = myEyeVectorY * ( myVectorEyetoMiddle.normalize().length() * tan(m_fovRad) / myEyeVectorY.length() ); //V RGBApixel BGColor; BGColor.Red = (ebmpBYTE) m_bgcolor[0]*255; BGColor.Green = (ebmpBYTE) m_bgcolor[1]*255; BGColor.Blue = (ebmpBYTE) m_bgcolor[2]*255; BGColor.Alpha = 0; RGBApixel BoxColor; BoxColor.Red = (ebmpBYTE) m_boxcolor[0]*255; BoxColor.Green = (ebmpBYTE) m_boxcolor[1]*255; BoxColor.Blue = (ebmpBYTE) m_boxcolor[2]*255; BoxColor.Alpha = 0; double UnitCube[2][3] = { {0, 0, -1}, {1, 1, 0} }; //cube double a1 = UnitCube[0][0] - m_position[0]; double a2 = UnitCube[1][0] - m_position[0]; double b1 = UnitCube[0][1] - m_position[1]; double b2 = UnitCube[1][1] - m_position[1]; double c1 = UnitCube[0][2] - m_position[2]; double c2 = UnitCube[1][2] - m_position[2]; for(unsigned int i=0;i<width;i++){ for(unsigned int j=0;j<height;j++){ double t1x, t2x, t1y, t2y, t1z, t2z; double sx,sy; sx = double(i)/width; sy = double(j)/height; Vector P = myImageVectorX*(2*sx-1) + myImageVectorY*(2*sy-1) + myMiddlePoint ; Vector myRayCasting = (P - m_position)*(1.0 / (P - m_position).normalize().length()); //RayDirection = (P - E)/normalize(P - E); t1x = a1 / myRayCasting[0]; t2x = a2 / myRayCasting[0]; t1y = b1 / myRayCasting[1]; t2y = b2 / myRayCasting[1]; t1z = c1 / myRayCasting[2]; t2z = c2 / myRayCasting[2]; bool hit = RayBoxIntersection( t1x, t2x, t1y, t2y, t1z, t2z ); if (!hit) file.SetPixel(i, height-j-1, BGColor); else file.SetPixel(i, height-j-1, BoxColor); } } file.WriteToFile(outputFile.c_str()); }
int main(int argc, char **argv) { if(argc!=2) { cout<<"Requires an argument specifying output file"; exit(0); } int width = 600, height = 600; /* Material Test */ Brdf br1 = {Vector3f(1.0, 0.0, 1.0), Vector3f(1.0, 1.0, 1.0), Vector3f(0.1, 0.1, 0.1), Vector3f(0.0, 0.0, 0.0), 50.0}; Material m1(br1); m1.ri = 1.52; Brdf br2 = {Vector3f(1.0, 1.0, 0.0), Vector3f(1.0, 1.0, 1.0), Vector3f(0.1, 0.1, 0.1), Vector3f(0.0, 0.0, 0.0), 50.0}; Material m2(br2); m2.ri = 1.52; Brdf br3 = {Vector3f(0.0, 1.0, 1.0), Vector3f(1.0, 1.0, 1.0), Vector3f(0.1, 0.1, 0.1), Vector3f(0.0, 0.0, 0.0), 50.0}; Material m3(br3); m3.ri = 1.52; Brdf br4 = {Vector3f(0.1, 0.1, 0.1), Vector3f(1.0, 1.0, 1.0), Vector3f(0.1, 0.1, 0.1), Vector3f(1.0, 1.0, 1.0), 50.0}; Material m4(br4); m4.ri = 1.52; /* Primitive test */ Sphere sp = Sphere(Vector3f(0, 0, 20), Vector3f(1,0,0), 3.0); GeoPrimitive g(&sp, &m1); Vector3f vertices[3]; vertices[0] = Vector3f(-5, 5, 17); vertices[1] = Vector3f(-1, 4, 20); vertices[2] = Vector3f(-6, -1, 20); Triangle tr = Triangle(vertices, Vector3f(1,0,0)); GeoPrimitive t(&tr, &m4); Brdf* temp; LocalGeo temp2; Sphere sp2 = Sphere(Vector3f(2, 2, 15), Vector3f(0,1,0), 1.0); GeoPrimitive g2(&sp2, &m2); Sphere sp3 = Sphere(Vector3f(2, -0.5, 15), Vector3f(0,0,1), 1.0); GeoPrimitive g3(&sp3, &m3); //Sphere sp4 = Sphere(Vector3f(0,-1008,10), Vector3f(0,0,1), 1000.0); //GeoPrimitive g4(&sp4, &m4); /* Light Test */ DirectionalLight l = DirectionalLight(Vector3f(1.0, 1.0, 1.0), Vector3f(0.0, 0.0, 0.0), Vector3f(0.57735, -0.57735, -0.57735)); PointLight l1 = PointLight(Vector3f(1.0, 1.0, 1.0), Vector3f(1.0, 1.0, 1.0), Vector3f(0.0, -100.0, 0.0)); PointLight l2 = PointLight(Vector3f(1.0, 1.0, 1.0), Vector3f(1.0, 1.0, 1.0), Vector3f(0.0, 100.0, 0.0)); //DirectionalLight l2 = DirectionalLight(Vector3f(0.0, 0.0, 1.0), Vector3f(0.0, 0.0, 0.0), Vector3f(0.57735, -0.57735, -0.57735)); /* Camera test */ Sample s; s.x = 0; s.y = 0; Camera c(Vector3f(0, 0, 5), Vector3f(0,0,15), Vector3f(0,1,0), pi/3, pi/3, width, height); /* Sampler test */ Sampler smp(width, height); smp.getSample(); s = *(smp.sample); /* Scene test */ Scene sc; sc.addPrimitiveToScene(&g); sc.addPrimitiveToScene(&g2); sc.addPrimitiveToScene(&g3); //sc.addPrimitiveToScene(&g4); sc.addPrimitiveToScene(&t); sc.addLightToScene(&l1); sc.addLightToScene(&l2); /* Generating output bitmap */ BMP image; image.SetSize(width, height); image.SetBitDepth(24); /* Ray Tracer multi trace test*/ Ray r; Vector3f color; RayTracer rt(10, &sc, &c); cout<<"\n --------- RayTracer Multi Trace----------\n"; while(smp.getSample()) { c.generateRay(*(smp.sample), &r); //cout<<"Ray - \n Origin = "<<r.origin<<"\nDirection = "<<r.direction<<"\n"; color = {0.0, 0.0, 0.0}; //cout<<"X = "<<smp.sample->x<<" Y = "<<smp.sample->y<<"\n"; rt.trace(r, 0, &color); //cout<<"\n-----Color = "<<color<<"\n\n"; for (int i = 0; i<3; i++) { if (color[i]>1.0) color[i] = 1.0; else if (color[i]<0.0) color[i] = 0.0; } image(smp.sample->x, smp.sample->y)->Red = color[0]*255; image(smp.sample->x, smp.sample->y)->Green = color[1]*255; image(smp.sample->x, smp.sample->y)->Blue = color[2]*255; } image.WriteToFile(argv[1]); return 0; }
int main(int argc, char** argv) { unsigned int width = 800; unsigned int height = 600; float aspect = ((float)width)/height; float fovy = 90.0f * 3.1415926535/180; float phi = fovy/2; vec4 up = vec4(0,1,0,0); vec4 u = vec4(-1,0,0,0); vec4 v = up * tan(phi); vec4 h = -u * tan(phi) * aspect; vec4 m = vec4(0,0,-1,1); vec4 e = vec4(0,0,0,1); BMP output; output.SetSize(width, height); output.SetBitDepth(24); for(unsigned int x = 0; x < width; x++) { for(unsigned int y = 0; y < height; y++) { Ray ray; vec4 p = m + (2 * (float)x/(width-1)-1)*h + (2 * (float)y/(height-1)-1) * v; ray.origin = e; ray.direction = (p-e)/length(p-e); output(x, y)->Red = abs(ray.direction.x)*255; output(x, y)->Green = abs(ray.direction.y)*255; output(x, y)->Blue = abs(ray.direction.z)*255; /*if(x % 40 == 0) { if(y % 40 == 0) { output(x, y)->Red = 255; output(x, y)->Blue = 0; output(x, y)->Green = 0; } else { output(x, y)->Red = 0; output(x, y)->Blue = 255; output(x, y)->Green = 0; } } else { if(y % 40 == 0) { output(x, y)->Red = 0; output(x, y)->Blue = 255; output(x, y)->Green = 0; } else { output(x, y)->Red = 255; output(x, y)->Blue = 0; output(x, y)->Green = 0; } }*/ } } output.WriteToFile("output.bmp"); return 0; }