void drawPoints(const string &aImageFilename0, const string &aPointsFilename0, const string &aImageFilename1, const string &aPointsFilename1, const string &aMatchFilename) { Im2D_U_INT1 red0, green0, blue0; getGrayImages(aImageFilename0, red0, green0, blue0); drawDetectedPoints(aPointsFilename0, blue0); Im2D_U_INT1 red1, green1, blue1; getGrayImages(aImageFilename1, red1, green1, blue1); drawDetectedPoints(aPointsFilename1, blue1); drawMatches(aMatchFilename, green0, green1); string dstImageFilename0 = drawn_matches_filename(aMatchFilename, aImageFilename0); save_tiff(dstImageFilename0, red0, green0, blue0); string dstImageFilename1 = drawn_matches_filename(aMatchFilename, aImageFilename1); save_tiff(dstImageFilename1, red1, green1, blue1); }
void save_img(std::string filename, const Matrix<Color>& img) { std::size_t n = filename.size(); std::string extension3 = filename.substr(n-3, n); std::string extension4 = filename.substr(n-4, n); if(!extension3.compare("bmp")) { save_bmp(filename, img); } else if(!extension3.compare("gif")) { save_gif(filename, img); } else if(!extension3.compare("ico")) { save_ico(filename, img); } /*else if(!extension3.compare("jpg")) { save_jpeg(filename, img); }*/ else if(!extension3.compare("pcx")) { save_pcx(filename, img); } else if(!extension3.compare("png")) { save_png(filename, img); } else if(!extension3.compare("pbm")) { save_pbm(filename, color2bwimage(img)); } else if(!extension3.compare("pgm")) { save_pgm(filename, color2grayimage(img)); } else if(!extension3.compare("ppm")) { save_ppm(filename, img); } else if(!extension3.compare("tga")) { save_tga(filename, img); } else if(!extension4.compare("tiff")) { save_tiff(filename, img); } }
void bakeToTiff(const char* in, const char* tiffname, int bake) { FILE * bakefile; TqUshort h, w; TqFloat s, t, r1, g1, b1; TqFloat *pixels; TqFloat *xpixels; TqFloat *filter, *pf; TqChar buffer[ 200 ]; TqInt i, k, n; TqInt x, y; TqFloat mins, mint, maxs, maxt; bool normalized = true; TqInt count, number; TqFloat *temporary; TqInt elmsize = 3; TqFloat invWidth, invSize; count = 1024 * 1024; number = 0; h = w = bake; pixels = ( TqFloat * ) calloc( 4, bake * bake * sizeof(TqFloat)); temporary = (TqFloat *) malloc(count * 5 * sizeof(TqFloat)); bakefile = fopen( in, "rb" ); /* Ignore the first line of text eg. Aqsis bake file */ if(std::fgets( buffer, 200, bakefile ) == NULL) AQSIS_THROW_XQERROR(XqInternal, EqE_BadFile, "Could not read bake file header"); // Read the second line of text to configure how many floats we expect to // read if(std::fgets( buffer, 200, bakefile ) == NULL || std::sscanf(buffer, "%d", &elmsize) == 0) AQSIS_THROW_XQERROR(XqBadTexture, EqE_BadFile, "Could not read length of bake file \"" << in << "\""); while ( std::fgets( buffer, 200, bakefile ) != NULL ) { k = number * 5; switch (elmsize) { case 3: { sscanf( buffer, "%f %f %f %f %f", &s, &t, &r1, &g1, &b1 ); } break; case 2: { sscanf( buffer, "%f %f %f %f", &s, &t, &r1, &g1); b1 = (r1 + g1) / 2.0; } break; default: case 1: { sscanf( buffer, "%f %f %f", &s, &t, &r1); g1 = b1 = r1; } break; } temporary[k] = s; temporary[k+1] = t; temporary[k+2] = r1; temporary[k+3] = g1; temporary[k+4] = b1; number++; if (number >= (count - 1)) { count += 1024; temporary = (float *) realloc((void *) temporary, count * 5 * sizeof(float)); } } /* printf("done\nFind the max, min of s,t.\n"); */ mins = maxs = temporary[0]; mint = maxt = temporary[1]; /* Find the min,max of s and t */ for (i=0; i < number; i++) { k = i * 5; if (mins > temporary[k]) mins = temporary[k]; if (mint > temporary[k+1]) mint = temporary[k+1]; if (maxs < temporary[k]) maxs = temporary[k]; if (maxt < temporary[k+1]) maxt = temporary[k+1]; } if ((mins >= 0.0 && maxs <= 1.0) && (maxt >= 0.0 && maxt <= 1.0) ) { normalized = false; } if (normalized == true) { Aqsis::log() << "bake2tif normalizes the keys (normally s,t)" << std::endl; Aqsis::log() << "\t(min_s, max_s): (" << mins << ", " << maxs << "), (min_t, max_t): (" << mint << ", " << maxt << ")" << std::endl; } /* Try to adjust with the final resolution of mipmap */ /* filter_size = max((int) log((double)bake)/log(2.0) + 2, (int) FILTER_TBL_SIZE); * filter_size = (int) ceil(log((double)min((int) ( (maxs - mins) * bake), (int) ((maxt -mint ) * bake)))/log(2.0)); */ invWidth = 1.0f/filter_width; invSize = 1.0f/filter_size; /* init the filter' table */ filter = (TqFloat *) calloc(filter_size*filter_size,sizeof(TqFloat)); pf = filter; for (y=0; y < filter_size; ++y) { TqFloat fy = (TqFloat)( y+ 0.5f) * filter_width * invSize; for (x=0; x < filter_size; ++x) { TqFloat fx = ((TqFloat) x+ 0.5f) * filter_width * invSize; /* we will use a disk filter the point will dispose in * a circle around each point; more pleasant visually */ *pf++ = RiDiskFilter(fx,fy,filter_width, filter_width); } } /* Now it is time to save s,t, r1, g1, b1 into pixels array (along with the sum/area filtering accumalor */ for (i=0; i < number; i++) { TqInt x0, x1, y0, y1; TqInt *ifx, *ify; TqFloat dImageX; TqFloat dImageY; k = i * 5; s = temporary[k]; t = temporary[k+1]; r1 = temporary[k+2]; g1 = temporary[k+3]; b1 = temporary[k+4]; /* printf("%d\n", number); */ /* Normalize the s,t between 0..1.0 only if required */ if (normalized) { if ( (maxs - mins) != 0.0) { s = (s - mins) / (maxs - mins); } else { if (s < 0.0) s *= -1.0; if (s > 1.0) s = 1.0; } if ((maxt - mint) != 0.0) { t = (t - mint) / (maxt - mint); } else { if (t < 0.0) t *= -1.0; if (t > 1.0) t = 1.0; } } /* When we have some collision ? What should be nice * to accumulate the RGB values instead ? */ x = (int)( s * ( bake - 1 ) ); y = (int)( t * ( bake - 1 ) ); /* printf("x %d y %d rgb %f %f %f\n", x, y, r1, g1, b1); */ /* each each pixels accumulated it in xpixels but * make sure we use * a filtering of 16x16 to garantee spreading of the * values across x,y pixels. */ dImageX = x - 0.5f; dImageY = y - 0.5f; x0 = (TqInt) ceil(dImageX - filter_width); x1 = (TqInt) floor(dImageX + filter_width); y0 = (TqInt) ceil(dImageY - filter_width); y1 = (TqInt) floor(dImageY + filter_width); x0 = max(x0, 0); x1 = min(x1, bake -1); y0 = max(y0, 0); y1 = min(y1, bake -1); if ( ( (x1-x0)<0) || ((y1-y0)<0 )) continue; /* filter delta indexes*/ ifx = (TqInt*)calloc(x1-x0+1, sizeof(TqInt)); for (x = x0; x <= x1; ++x) { TqFloat fx = fabsf(x -dImageX) * invWidth * filter_size; ifx[x-x0] = min((int) floor(fx), filter_size - 1); } ify = (TqInt*)calloc(y1-y0+1, sizeof(TqInt)); for (y = y0; y <= y1; ++y) { TqFloat fy = fabsf(y -dImageY) * invWidth * filter_size; ify[y-y0] = min((int) floor(fy), filter_size - 1); } /* Fill all the right pixels now */ for (y = y0; y < y1; ++y) { for (x = x0; x <= x1; ++x) { TqInt offset; TqFloat filterWt; offset = ify[y-y0]*filter_size + ifx[x-x0]; /* printf("offset %d ", offset); */ filterWt = filter[offset]; /* Remove the negative lob maybe ? * if (filterWt < 0.0) continue; */ /* printf("wt %f\n", filterWt); */ n = (y * bake + x); n *= 4; pixels[n] += (filterWt * r1); pixels[n+1] += (filterWt * g1); pixels[n+2] += (filterWt * b1); pixels[n+3] += filterWt; /* printf("x %d y %d rgb wt %f %f %f %f\n", x, y, pixels[n], pixels[n+1], pixels[n+2], pixels[n+3]); */ } } free(ifx); free(ify); } /* Now it is time to unroll the filterWt and save into xpixels */ xpixels = ( TqFloat * ) calloc( 3, bake * bake * sizeof(TqFloat)); //CqTextureBuffer<TqFloat> xpixels(bake, bake, 3); for (y=0; y < bake; ++y) { for (x=0; x < bake; ++x) { TqInt m; n = (y * bake + x); m = n; m *= 3; n *= 4; if (pixels[n+3] > 0.0) { /* printf("unroll weightSum factor \n"); */ float area = 1.0/pixels[n+3]; xpixels[m] = pixels[n] * area; xpixels[m+1] = pixels[n+1] * area; xpixels[m+2] = pixels[n+2] * area; } } } save_tiff( tiffname, xpixels, w, h, 3, "Aqsis Renderer, bake2tif conversion"); free( pixels ); free( xpixels ); free( filter ); free(temporary); fclose( bakefile ); }