Apple::Apple() {
	Vector3 p0 = Vector3( 0, 0, 0 );		Vector3 p1 = Vector3( -.2, -1, 0 ); 
	Vector3 p2 = Vector3( -1.5, -.5, 0 );	Vector3 p3 = Vector3( -2, 1, 0 );
	Vector3 p4 = Vector3( -3, 2.5, 0 );		Vector3 p5 = Vector3( -.5, 4, 0 );
	Vector3 p6 = Vector3( 0, 2, 0 );

	controlPoints.push_back( p0 ); controlPoints.push_back( p1 );
	controlPoints.push_back( p2 ); controlPoints.push_back( p3 );
	controlPoints.push_back( p4 ); controlPoints.push_back( p5 );
	controlPoints.push_back( p6 );
	
	bezierCurve( 10, 10, controlPoints, objPoints, objNormals, objTexCoords );
	
	ambient[0] = 1.; ambient[1] = .0f; ambient[2] = .0f; ambient[3] = 1.0f;
	diffuse[0] = 1.f; diffuse[1] = .0f; diffuse[2] = .0f; diffuse[3] = 1.0f;
	specular[0] = 1.; specular[1] = 1.; specular[2] = 1.; specular[3] = 1.0f;
	shininess[0] = 128;	
	
	int w = 256;
	texture = loadPPM( "apple.ppm", w, w );
	glGenTextures( 1, textureID );
	glBindTexture( GL_TEXTURE_2D, textureID[0] );
	glTexImage2D( GL_TEXTURE_2D, 0, 3, w, w, 0, GL_RGB, GL_UNSIGNED_BYTE, texture );	
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
}
Exemple #2
0
  /*! loads an image from a file with auto-detection of format */
  Ref<Image> loadImageFromDisk(const FileName& fileName) try
  {
    std::string ext = std::strlwr(fileName.ext());
#ifdef USE_OPENEXR
    if (ext == "exr" ) return loadExr(fileName);
#endif
#ifdef USE_IMAGEMAGICK
    if (ext == "bmp" ) return loadMagick(fileName);
    if (ext == "gif" ) return loadMagick(fileName);
    if (ext == "png" ) return loadMagick(fileName);
    if (ext == "tga" ) return loadMagick(fileName);
    if (ext == "tif" ) return loadMagick(fileName);
    if (ext == "tiff") return loadMagick(fileName);
#endif
#ifdef USE_LIBJPEG
    if (ext == "jpg" ) return loadJPEG(fileName);
#endif
    if (ext == "pfm" ) return loadPFM(fileName);
    if (ext == "ppm" ) return loadPPM(fileName);
    throw std::runtime_error("image format " + ext + " not supported");
  }
  catch (const std::exception& e) {
    std::cout << "cannot read file " << fileName << ": " << e.what() << std::endl;
    return null;
  }
Bowl::Bowl() {
	Vector3 p0 = Vector3( 0, 0, 0 );		Vector3 p1 = Vector3( -3, .3, 0 );
	Vector3 p2 = Vector3( -5, .3, 0 );		Vector3 p3 = Vector3( -5, 4, 0 );
	Vector3 p4 = Vector3( -4.8, 5, 0 );		Vector3 p5 = Vector3( -4.8, 3, 0 );
	Vector3 p6 = Vector3( -3.5, 2, 0 );		Vector3 p7 = Vector3( -2.f, .5f, 0.f );
	Vector3 p8 = Vector3( -1.f, .5f, 0.f ); Vector3 p9 = Vector3( 0.f, .5, 0.f );
	
	controlPoints.push_back( p0 ); controlPoints.push_back( p1 );
	controlPoints.push_back( p2 ); controlPoints.push_back( p3 );
	controlPoints.push_back( p4 ); controlPoints.push_back( p5 );
	controlPoints.push_back( p6 ); controlPoints.push_back( p7 );
	controlPoints.push_back( p8 ); controlPoints.push_back( p9 );
	
	bezierCurve( 10, 100, controlPoints, objPoints, objNormals, objTexCoords );
	
	ambient[0] = .3f; ambient[1] = .3f; ambient[2] = .3f; ambient[3] = 1.0f;
	diffuse[0] = .8f; diffuse[1] = .5f; diffuse[2] = .24f; diffuse[3] = 1.0f;
	specular[0] = 0; specular[1] = 0; specular[2] = 0; specular[3] = 1.0f;
	shininess[0] = 0;		

	int w = 256;
	texture = loadPPM( "bowl.ppm", w, w );
	glGenTextures( 1, textureID );
	glBindTexture( GL_TEXTURE_2D, textureID[0] );
	glTexImage2D( GL_TEXTURE_2D, 0, 3, w, w, 0, GL_RGB, GL_UNSIGNED_BYTE, texture );	
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

}
////////////////////////////////////////////////////////////////////////////////
//! Load PPM image file (with unsigned char as data element type), padding 4th component
//! @return CUTTrue if reading the file succeeded, otherwise false
//! @param file  name of the image file
//! @param data  handle to the data read
//! @param w     width of the image
//! @param h     height of the image
////////////////////////////////////////////////////////////////////////////////
int 
cutLoadPPM4ub( const char* file, unsigned char** data, 
               unsigned int *w,unsigned int *h)
{
    unsigned char *idata = 0;
    unsigned int channels;
    
    if (loadPPM( file, &idata, w, h, &channels)) {
        // pad 4th component
        int size = *w * *h;
        // keep the original pointer
        unsigned char* idata_orig = idata;
        *data = (unsigned char*) malloc( sizeof(unsigned char) * size * 4);
        unsigned char *ptr = *data;
	int i;
        for(i=0; i<size; i++) {
            *ptr++ = *idata++;
            *ptr++ = *idata++;
            *ptr++ = *idata++;
            *ptr++ = 0;
        }
        free( idata_orig);
        return CUTTrue;
    }
    else
    {
        cutFree( idata);
        return CUTFalse;
    }
}
////////////////////////////////////////////////////////////////////////////////
//! Load PPM image file (with unsigned char as data element type)
//! @return CUTTrue if reading the file succeeded, otherwise false
//! @param file  name of the image file
//! @param data  handle to the data read
//! @param w     width of the image
//! @param h     height of the image
////////////////////////////////////////////////////////////////////////////////
int 
cutLoadPPMub( const char* file, unsigned char** data, 
              unsigned int *w,unsigned int *h)
{
    unsigned int channels;
    return loadPPM( file, data, w, h, &channels);
}
Mushroom::Mushroom() {
	Vector3 p0 = Vector3( 0, 0, 0 );		Vector3 p1 = Vector3( -1, .2, 0 );
	Vector3 p2 = Vector3( -1.1, .5, 0 );	Vector3 p3 = Vector3( -1, 1, 0 );
	Vector3 p4 = Vector3( -.9, 1.5, 0 );	Vector3 p5 = Vector3( -2, 1.5, 0 );
	Vector3 p6 = Vector3( -2.5f, 1.2f, 0 );	Vector3 p7 = Vector3( -2.8f, 1.f, 0.f );
	Vector3 p8 = Vector3( -3.f, 2.f, 0.f );	Vector3 p9 = Vector3( -2.5f, 2.5f, 0.f );
	Vector3 p10 = Vector3( -2.f, 3.f, 0.f );	Vector3 p11 = Vector3( -1.f, 3.5f, 0.f );
	Vector3 p12 = Vector3( 0, 3.2f, 0.f );	
	
	controlPoints.push_back( p0 ); controlPoints.push_back( p1 );
	controlPoints.push_back( p2 ); controlPoints.push_back( p3 );
	controlPoints.push_back( p4 ); controlPoints.push_back( p5 );
	controlPoints.push_back( p6 ); controlPoints.push_back( p7 );
	controlPoints.push_back( p8 ); controlPoints.push_back( p9 );
	controlPoints.push_back( p10 ); controlPoints.push_back( p11 );
	controlPoints.push_back( p12 );
	
	bezierCurve( 10, 20, controlPoints, objPoints, objNormals, objTexCoords );
	
	ambient[0] = .5f; ambient[1] = .5f; ambient[2] = .5f; ambient[3] = 1.0f;
	diffuse[0] = .82f; diffuse[1] = .41f; diffuse[2] = .12f; diffuse[3] = 1.0f;
	specular[0] = 0; specular[1] = 0; specular[2] = 0; specular[3] = 1.0f;
	shininess[0] = 0;

	int w = 256;
	texture = loadPPM( "bowl.ppm", w, w );
	glGenTextures( 1, textureID );
	glBindTexture( GL_TEXTURE_2D, textureID[0] );
	glTexImage2D( GL_TEXTURE_2D, 0, 3, w, w, 0, GL_RGB, GL_UNSIGNED_BYTE, texture );	
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
}
GLuint WaterFrameBuffer::getTextureFromPPM(const GLchar * file)
{
	GLuint textureID;
	glGenTextures(1, &textureID);

	int twidth, theight;

	unsigned char* tdata;

	glBindTexture(GL_TEXTURE_2D, textureID);

	tdata = loadPPM(file, twidth, theight);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);

	std::cerr << "width: " << twidth << " height: " << theight << std::endl;

	glGenerateMipmap(GL_TEXTURE_2D);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -0.4f);

	glBindTexture(GL_TEXTURE_2D, 0);

	return textureID;
}
Exemple #8
0
void loadTexture(const char* name, int id)
{
	//GLuint texture[1];     // storage for one texture
	int twidth, theight;   // texture width/height [pixels]
	unsigned char* tdata;  // texture pixel data

	// Load image file
	tdata = loadPPM(name, twidth, theight);
	if (tdata == NULL) return;

	// Create ID for texture
	glGenTextures(1, &SkyboxTexture[id]);

	// Set this texture to be the one we are working with
	glBindTexture(GL_TEXTURE_2D, SkyboxTexture[id]);

	// Generate the texture
	glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);

	// Make sure no bytes are padded:
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	// Select GL_MODULATE to mix texture with polygon color for shading:
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// Set bi-linear filtering for both minification and magnification
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
Exemple #9
0
// LoadsGLfloat a[]={A[0]-O[0], A[1]-O[1], A[2]-O[2]}; a simple texture
GLuint loadTexture(const std::string &fileName)
{
    GLuint  w;
    GLuint  h;
    // Loads the image from a ppm file to an unsigned char array
    unsigned char *data=loadPPM(fileName, &w, &h);

    // Allocates a texture id
    GLuint textureID;
    glGenTextures(1, &textureID);
    // Selects our current texture
    glBindTexture(GL_TEXTURE_2D, textureID);

    // How to handle not normalised uvs
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    // How to handle interpolation from texels to fragments
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    // Specifies which image will be used for this texture objet
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    
    return textureID;
}
Exemple #10
0
// Loads a cubemap texture from 6 individual texture faces
// Order should be:
// +X (right)
// -X (left)
// +Y (top)
// -Y (bottom)
// +Z (front)? (CHECK THIS)
// -Z (back)?
CubeMapTexture::CubeMapTexture(
	const char* rtTex, const char* ltTex,
	const char* tpTex, const char* dnTex,
	const char* ftTex, const char* bkTex, bool repeatTex_on) {

	std::vector<const char*> fv;
	fv.push_back(rtTex);
	fv.push_back(ltTex);
	fv.push_back(tpTex);
	fv.push_back(dnTex);
	fv.push_back(ftTex);
	fv.push_back(bkTex);

	// create ID for texture
	GLuint tID;
	glGenTextures(1, &tID); // this func ensures the id is unique 
	textureID = tID;
	// set the currentTextureUnit to be default, could be skipped?
	glActiveTexture(GL_TEXTURE0);

	int twidth, theight;   // texture width/height [pixels]
	unsigned char* tdata;  // texture pixel data

	// bind
	glBindTexture(GL_TEXTURE_CUBE_MAP, tID);

	//glUniform1i(0, 0);

	for (GLuint i = 0; i < fv.size(); i++)
	{
		tdata = loadPPM(fv[i], twidth, theight);

		//If the image wasn't loaded, can't continue
		if (tdata == NULL) {
			std::cerr << "no texture loaded\n";
			return;
		}

		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
	}

	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	
	if (repeatTex_on) {
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_REPEAT);

	}
	else {
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
	}

	// unbind
	glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
GLuint skyBox:: loadCubemap(){
    glGenTextures(1, &textureID);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
    unsigned char* image;
    //std::vector<unsigned char*> faces = convert_img_ppm();
    int width, height;
    image = loadPPM(miramar_lf, width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM(miramar_rt, width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+1, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM(miramar_up, width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+2, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM(miramar_dn, width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+3, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM(miramar_ft, width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+4, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM(miramar_bk, width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+5, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
    
    cubemapTexture = textureID;
    return textureID;
}
Exemple #12
0
// load image file into texture object
GLuint SkyBox::loadSkyBox(std::vector<const GLchar*> faces)
{
	// Memory for texture
	GLuint textureID;
	glGenTextures(1, &textureID);

	// Texture's width and height
	int twidth, theight;

	// Texture pixel data from loading ppm
	unsigned char* tdata;

	// We will be binding to cube map
	glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);

	// Loop through each face and process it
	for (GLuint i = 0; i < faces.size(); ++i)
	{
		// Load image from ppm to texture data
		tdata = loadPPM(faces[i], twidth, theight);
		if (tdata == NULL) { std::cerr << "Failed to load skybox face!" << std::endl; exit(1); }

		// Generate the texture
		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
	}

	/*
	// Make sure no bytes are padded:
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	// Select GL_MODULATE to mix texture with polygon color for shading:
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// Use bilinear interpolation:
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	// Use clamp to edge to hide skybox edges:
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	*/
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
	
	// Free texture?
	glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

	return textureID;
}
GLuint Terrain:: loadCubemap(){
    glGenTextures(1, &textureID);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
    
    int width, height;
    
    image = loadPPM(normal_ppm, width, height); //raw data
    
    
    
    cubemapTexture = textureID;
    return textureID;
}
GLuint Texture::loadTexture(const char * filename)
{
	GLuint textureID;
	glGenTextures(1, &textureID);

	int width, height;
	unsigned char* image;

	glBindTexture(GL_TEXTURE_2D, textureID);
	image = loadPPM(filename, width, height);

	if (image == NULL) {
		printf("Texture was null!\n");
		delete(image);
	}

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB,
		width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

	delete(image);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	if (!strcmp(filename, "Particle-Texture.ppm")) {
		printf("particle\n");
	}
	else {
		printf("not particle\n");
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	

	printf("Building texture: %d\n", textureID);

	return textureID;
}
Exemple #15
0
GLuint loadCubemapCross(char *filename)
{
    Image *cross = loadPPM(filename);
    if (!cross) {
        return 0;
    }
    flipImage(cross);

    Image *faces[6];
    bool success = convertCrossToCubemap(cross, faces);
    if (success) {
        GLuint tex = createCubemapTexture(faces);
        return tex;
    }

    return 0;
}
Exemple #16
0
RawImage::RawImage(const char *path) throw(ImageException) :
  _type(GL_RGB),
  _texId(0),
  _bytesPerPixel(0),
  _width(0),
  _height(0),
  _pixels(NULL)
{
  const char *filename = basename(const_cast<char *>(path));
  if (filename == NULL)
    throw ImageException("Invalid image filename: %s does not name a file.", filename);

  const char *ext = strrchr(filename, '.');
  if (ext == NULL)
    throw ImageException("Unknown image format.");

  FILE *file = fopen(path, "rb");
  if (file == NULL)
    throw ImageException("File not found: %s.", filename);

  try {
    if (strcasecmp(ext, ".bmp") == 0) {
      loadBMP(file);
    } else if (strcasecmp(ext, ".tga") == 0) {
      loadTGA(file);
    } else if (strcasecmp(ext, ".ppm") == 0) {
      loadPPM(file);
    } else if (strcasecmp(ext, ".jpg") == 0 || strcasecmp(ext, ".jpeg") == 0) {
      loadJPG(file);
    } else if (strcasecmp(ext, ".png") == 0) {
      loadPNG(file);
    } else if (strcasecmp(ext, ".tif") == 0 || strcasecmp(ext, ".tiff") == 0) {
      loadTIFF(path);
    } else {
      throw ImageException("Unknown image format: %s", ext);
    }
    fclose(file);
  } catch (ImageException& ex) {
    fclose(file);
    if (_pixels != NULL)
      delete _pixels;
    throw ex;
  }
}
Exemple #17
0
Texture::Texture(const char* fname, bool repeat_on)
{
    filename = fname;
    
    GLuint texture[1];     // storage for one texture
    int twidth, theight;   // texture width/height [pixels]
    unsigned char* tdata;  // texture pixel data
    
    //Load image file
    tdata = loadPPM(filename, twidth, theight);
    
    //If the image wasn't loaded, can't continue
    if(tdata == NULL)
        return;
    
    //Create ID for texture
    glGenTextures(1, &texture[0]);
    id=texture[0];
    
    //Set this texture to be the one we are working with
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    
    //Generate the texture
    glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
    
    //Make sure no bytes are padded:
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    
    //Select GL_MODULATE to mix texture with quad color for shading:
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    
	if (repeat_on) {
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	}

    //Use bilinear interpolation:
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    //And unbind it!
    glBindTexture(GL_TEXTURE_2D, 0);
}
Exemple #18
0
void loadTexture(){
    GLuint texture[1];     // storage for one texture
    int twidth, theight;   // texture width/height [pixels]
    unsigned char* tdata;  // texture pixel data
    
    // Load image file
    tdata = loadPPM("Brickwall.ppm", twidth, theight);
    if (tdata==NULL) return;
    
    // Create ID for texture
    glGenTextures(1, &texture[0]);
    
    // Set this texture to be the one we are working with
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    
    // Generate the texture
    glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
    
    // Set bi-linear filtering for both minification and magnification
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
Exemple #19
0
GLuint loadCubemap(char *filenameFormat)
{
    // load faces
    Image *faces[6];
    for(int i=0; i<6; i++) {
        char filename[256];
        sprintf(filename, filenameFormat, i+1);
        faces[i] = loadPPM(filename);
		if (!faces[i])
			return 0;
        flipImage(faces[i]);
    }

    GLuint tex = createCubemapTexture(faces);

    for(int i=0; i<6; i++) {
        free(faces[i]->data);
        free(faces[i]);
    }

    return tex;
}
Exemple #20
0
void Texture::loadTexture(GLuint id, const char* the_texture) {
	int twidth, theight;   // texture width/height [pixels]
	unsigned char* tdata;  // texture pixel data

	//glUniform1i(glGetUniformLocation(gl2Program, "texture"), 0);

	// Load image file
	tdata = loadPPM(the_texture, twidth, theight);
	if (tdata == NULL) cout << "[Texture.cpp] Error loading PPM : " << the_texture << endl;

	// Set this texture to be the one we are working with
	glBindTexture(GL_TEXTURE_2D, id);

	// Generate the texture
	glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);

	// Set bi-linear filtering for both minification and magnification
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
}
Exemple #21
0
int main(int argc, char **argv) {
  if (argc != 6) {
    fprintf(stderr, "usage: %s sigma k min input(ppm) output(ppm)\n", argv[0]);
    return 1;
  }
  
  float sigma = atof(argv[1]);
  float k = atof(argv[2]);
  int min_size = atoi(argv[3]);
	
  /* printf("loading input image.\n"); */
  image<rgb> *input = loadPPM(argv[4]);
	
  /* printf("processing\n"); */
  int num_ccs; 
  image<rgb> *seg = segment_image(input, sigma, k, min_size, &num_ccs); 
  savePPM(seg, argv[5]);

  /* printf("got %d components\n", num_ccs); */
  /* printf("done! uff...thats hard work.\n"); */

  return 0;
}
int main(int argc, char **argv) {
  if (argc < 6) {
    fprintf(stderr, "usage: %s  nbimages ratio_horizontal ratio_vertical input1 (without .ppm) input2 (without .ppm) \n", argv[0]);
    return 1;
  }
  // (1) variables declarations
  char * imname = new char[100];
  char * imname2 = new char[100];
  char * outname = new char[100];
  char * appel = new char[1000];

 // (2) reading arguments
  int nb_images = atoi(argv[1]);
  int start=1;
  int i;
  sprintf(imname, "%s0%004d.ppm", argv[4], start);
  sprintf(imname2, "%s0%004d.ppm", argv[5], start);
  int ratio_horizontal = atoi(argv[2]);  
  int ratio_vertical = atoi(argv[3]);  
  printf("%s\n",imname);
  image<rgb> *input; image<rgb> *input2;   
  // (3) loading first image
  printf("loading input image.\n");
  input = loadPPM(imname);
  int width = input->width();
  int height = input->height();
  int N = width*height;
  
  
  image<rgb> *big = new image<rgb>(width*ratio_horizontal, height*ratio_vertical);
  
  
 for (i=start;i<=nb_images;i++) {
 
   sprintf(imname, "%s0%004d.ppm",argv[4], i); 
   sprintf(imname2, "%s0%004d.ppm",argv[5], i);   
   sprintf(outname, "%sout-0%004d.ppm", argv[5], i);
   input = loadPPM(imname);
   input2 = loadPPM(imname2);

   if(ratio_vertical==2){
      for (int y = 0; y < height; y++) {
	for (int x = 0; x < width; x++) {
	  imRef(big,x,y) = imRef(input, x,y);
	  imRef(big,x,y+height) = imRef(input2, x,y);
	}
      }
   }
   else  if(ratio_horizontal==2){
      for (int y = 0; y < height; y++) {
	for (int x = 0; x < width; x++) {
	  imRef(big,x,y) = imRef(input, x,y);
	  imRef(big,x+width,y) = imRef(input2, x,y);
	}
      }
   }
  
  
   savePPM(big, outname);
   sprintf(appel, "convert %s %s.png  ;", outname, outname );
   system(appel);
  }

  printf("done.\n");
  
  return 0;
}
int main(int argc, char **argv) {
  if (argc != 5) {
    fprintf(stderr, "usage: %s input (without .ppm) output (without .ppm) nbimages ratio \n", argv[0]);
    return 1;
  }
  // (1) variables declarations
  char * imname = new char[100];
  char * outname = new char[100];
  char * appel = new char[1000];

 // (2) reading arguments
  int nb_images = atoi(argv[3]);
  int start=1;
  int i;
  sprintf(imname, "%s0%004d.ppm", argv[1], start);
  sprintf(outname, "%s0%004d.ppm", argv[2], start);
  float ratio = atof(argv[4]);  
  printf("%s\n",imname);
  image<rgb> *input;  
  // (3) loading first image
  printf("loading input image.\n");
  input = loadPPM(imname);
  int width = input->width();
  int height = input->height();
  int N = width*height;
  
  
  
  image<rgb> *output= new image<rgb>((int)(width/ratio), (int)(height/ratio));
  
 for (i=start;i<=nb_images;i++) {
 
   sprintf(imname, "%s0%004d.ppm",argv[1], i); 
   sprintf(outname, "%s0%004d.ppm",argv[2], i);   
   input = loadPPM(imname);
   
   int moy_r, moy_g, moy_b, norm;
   for (int y = 0; y < height/2; y++) {
     for (int x = 0; x < width/2; x++) {
      moy_r = imRef(input, x*2,y*2).r;
      moy_g = imRef(input, x*2,y*2).g;
      moy_b = imRef(input, x*2,y*2).b;
      norm = 1;                      
      if (x*2+1<width) {
	moy_r += imRef(input, x*2+1,y*2).r;
	moy_g += imRef(input, x*2+1,y*2).g;
	moy_b += imRef(input, x*2+1,y*2).b;
	norm++;}                                  
      if (y*2+1<height) {
	moy_r += imRef(input, x*2,y*2+1).r; 
	moy_g += imRef(input, x*2,y*2+1).g; 
	moy_b += imRef(input, x*2,y*2+1).b; 
	norm++;}
      if ((x*2+1<width) && (y*2+1<height)) {
	moy_r +=  imRef(input, x*2+1,y*2+1).r;
	moy_g +=  imRef(input, x*2+1,y*2+1).g;
	moy_b +=  imRef(input, x*2+1,y*2+1).b;
	norm++; }
      imRef(output, x,y).r = moy_r/norm;
      imRef(output, x,y).g = moy_g/norm;
      imRef(output, x,y).b = moy_b/norm;
    }
  }
  
  savePPM(output, outname);

  }

  

  printf("done.\n");
  
  return 0;
}
Exemple #24
0
GLvoid InitGL(){
	//All the stuff we need to draw
	std::string hero_geo_file = "geo/tank.obj";
	std::string target_geo_file = "geo/target.obj";
	std::string swivel_geo_file = "geo/turret.obj";
	std::string barrel_geo_file = "geo/barrel.obj";
	std::string environment_geo_file = "geo/cube.obj";
	std::string enemy_geo_file = "geo/enemy.obj";
	std::string tile_geo_file = "geo/tile.obj";
	std::string tree_geo_file = "geo/tree.obj";
	std::string rock_geo_file = "geo/rocks.obj";

	std::string hero_tex_file = "tex/tank.ppm";
	std::string target_tex_file = "tex/target.ppm";
	std::string swivel_tex_file = "tex/turret.ppm";
	std::string barrel_tex_file = "tex/barrel.ppm";
	std::string environment_tex_file = "tex/cubemape.ppm";
	std::string enemy_tex_file = "tex/enemy.ppm";
	std::string tile_tex_file = "tex/naked_tile_baked.ppm";
	std::string tree_tex_file = "tex/tree.ppm";
	std::string rock_tex_file = "tex/rocks.ppm";
	std::string tree_tile_tex = "tex/tree_tile.ppm";
	std::string rock_tile_tex = "tex/rocky_tile.ppm";
	std::string blob_tile_tex = "tex/blob_shadow.ppm";

	mesh hero(hero_geo_file);
	mesh target(target_geo_file);
	mesh swivel(swivel_geo_file);
	mesh barrel(barrel_geo_file);
	mesh environment(environment_geo_file);
	mesh enemy(enemy_geo_file);
	mesh tile(tile_geo_file);
	mesh tree(tree_geo_file);
	mesh rock(rock_geo_file);
	HERO_ID = meshes.size();
	meshes.push_back(hero);
	TARGET_ID = meshes.size();
	meshes.push_back(target);
	SWIVEL_ID = meshes.size();
	meshes.push_back(swivel);
	BARREL_ID = meshes.size();
	meshes.push_back(barrel);
	ENVIRONMENT_ID = meshes.size();
	meshes.push_back(environment);
	ENEMY_ID = meshes.size();
	meshes.push_back(enemy);
	TILE_ID = meshes.size();
	meshes.push_back(tile);
	TREE_ID = meshes.size();
	meshes.push_back(tree);
	ROCK_ID = meshes.size();
	meshes.push_back(rock);

	glEnable(GL_TEXTURE_2D);
	glGenTextures(1, &HERO_TEX);
	glGenTextures(1, &TARGET_TEX);
	glGenTextures(1, &SWIVEL_TEX);
	glGenTextures(1, &BARREL_TEX);
	glGenTextures(1, &ENVIRONMENT_TEX);
	glGenTextures(1, &ENEMY_TEX);
	glGenTextures(1, &TILE_TEX);
	glGenTextures(1, &TREE_TEX);
	glGenTextures(1, &ROCK_TEX);
	glGenTextures(1, &TREETILE_TEX);
	glGenTextures(1, &ROCKTILE_TEX);
	glGenTextures(1, &BLOBTILE_TEX);
	loadPPM(hero_tex_file.c_str(), HERO_TEX);
	loadPPM(target_tex_file.c_str(), TARGET_TEX);
	loadPPM(swivel_tex_file.c_str(), SWIVEL_TEX);
	loadPPM(barrel_tex_file.c_str(), BARREL_TEX);
	loadPPM(environment_tex_file.c_str(), ENVIRONMENT_TEX);
	loadPPM(enemy_tex_file.c_str(), ENEMY_TEX);
	loadPPM(tile_tex_file.c_str(), TILE_TEX);
	loadPPM(tree_tex_file.c_str(), TREE_TEX);
	loadPPM(rock_tex_file.c_str(), ROCK_TEX);
	loadPPM(tree_tile_tex.c_str(), TREETILE_TEX);
	loadPPM(rock_tile_tex.c_str(), ROCKTILE_TEX);
	loadPPM(blob_tile_tex.c_str(), BLOBTILE_TEX, true);

	camera = new game_object(-1,-1,-1);

	game_object *hero_character = new game_object(characters.size(), HERO_ID, HERO_TEX);
	characters.push_back(hero_character);
	game_object *target_character = new game_object(characters.size(), TARGET_ID, TARGET_TEX);
	target_character->transform.translate(0.0,0.0,-7.0);
	characters.push_back(target_character);
	game_object *swivel_character = new game_object(characters.size(), SWIVEL_ID, SWIVEL_TEX);
	swivel_character->transform.translate(0.0,0.4,0.0);
	characters.push_back(swivel_character);
	game_object *barrel_character = new game_object(characters.size(), BARREL_ID, BARREL_TEX);
	barrel_character->transform.translate(0.0,0.0,-0.15);
	barrel_character->parent_to(characters[SWIVEL_ID]);
	characters.push_back(barrel_character);
	game_object *environment_character = new game_object(characters.size(), ENVIRONMENT_ID, ENVIRONMENT_TEX);
	environment_character->transform.scale(TILES_DIMENSION,TILES_DIMENSION,TILES_DIMENSION);
	environment_character->parent_to(camera);
	characters.push_back(environment_character);
	
	// game_object *enemy_character = new game_object(characters.size(), ENEMY_ID, ENEMY_TEX);
	// enemy_character->transform.translate(0.0, 0.0, -7.0);
	// enemy_character->parent_to(camera);
	// characters.push_back(enemy_character);
	// bad_guys.push_back(ENEMY_ID);

	srand(time(NULL));

	int spread = TILES_DIMENSION;
	for(int i=1; i<BAD_GUY_COUNT; i++)
	{
		game_object *enemyX = new game_object(characters.size(), ENEMY_ID, ENEMY_TEX);
		enemyX->transform.translate(rand()%spread-spread/2, 0.0, rand()%spread-spread/2);
		enemyX->transform.rotateY(rand()%180);
		enemyX->parent_to(camera);
		bad_guys.push_back(characters.size());
		colliders.push_back(characters.size());
		characters.push_back(enemyX);
	}

	double tile_width = meshes[TILE_ID].xmax - meshes[TILE_ID].xmin;
	double tile_length = meshes[TILE_ID].zmax - meshes[TILE_ID].zmin;
	for(int i=0; i<TILES_DIMENSION; i++)
	{
		int xmult = i - TILES_DIMENSION/2;
		for(int j=0; j<TILES_DIMENSION; j++)
		{
			int zmult = j - TILES_DIMENSION/2;
			double rot = rand() % 4 * 90.0;
			// std::cout << rot << std::endl;
			game_object *tileX = new game_object(characters.size(), TILE_ID, TILE_TEX);
			tileX->transform.rotateY(rot);
			tileX->transform.translate(tile_width*xmult, 0.0, tile_length*zmult);
			tileX->parent_to(camera);
			characters.push_back(tileX);
			int tile_type = rand() % 100;
			if(tile_type >= 96 && tile_type < 98)
			{
				tileX->tex = TREETILE_TEX;
				game_object *treeX = new game_object(characters.size(), TREE_ID, TREE_TEX);
				treeX->transform = tileX->transform;
				treeX->parent_to(camera);
				bad_guys.push_back(characters.size());
				colliders.push_back(characters.size());
				characters.push_back(treeX);
			}
			else if(tile_type >= 98)
			{
				tileX->tex = ROCKTILE_TEX;
				game_object *rockX = new game_object(characters.size(), ROCK_ID, ROCK_TEX);
				rockX->transform = tileX->transform;
				rockX->parent_to(camera);
				bad_guys.push_back(characters.size());
				colliders.push_back(characters.size());
				characters.push_back(rockX);
			}
			
		}
	}
	game_object *blob_character = new game_object(characters.size(), TILE_ID, BLOBTILE_TEX);
	blob_character->transform.translate(0.0,0.001,0.0);
	blob_character->transform.scale(2.0, 1.0, 3.0);
	characters.push_back(blob_character);

	last_update = (double)clock() / ((double)CLOCKS_PER_SEC);

	//GL boilerplate initialization
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.5f, 0.5f, 0.5f, 0.5f);				// grey Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing				

	glEnable(GL_CULL_FACE);
	glShadeModel(GL_SMOOTH);
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glEnable(GL_COLOR_MATERIAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glEnable (GL_BLEND);
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} 
Exemple #25
0
int main(int argc, char** argv) {
    if (argc != 9) {
        printf("%s c c_reg min sigma range hie_num input output\n", argv[0]);
        printf("       c --> value for the threshold function in over-segmentation\n");
        printf("   c_reg --> value for the threshold function in hierarchical region segmentation\n");
        printf("     min --> enforced minimum supervoxel size\n");
        printf("   sigma --> variance of the Gaussian smoothing.\n");
        printf("   range --> number of frames as one subsequence (k in the paper)\n");
        printf(" hie_num --> desired number of hierarchy levels\n");
        printf("   input --> input path of ppm video frames\n");
        printf("  output --> output path of segmentation results\n");
        return 1;
    }

    // Read Parameters
    float c = atof(argv[1]);
    float c_reg = atof(argv[2]);
    int min_size = atoi(argv[3]);
    float sigma = atof(argv[4]);
    int range = atoi(argv[5]);
    int hie_num = atoi(argv[6]);
    char* input_path = argv[7];
    char* output_path = argv[8];
    if (c <= 0 || c_reg <= 0 || min_size < 0 || sigma < 0 || hie_num < 0) {
        fprintf(stderr, "Uable to use the input parameters.");
        return 1;
    }
    printf("c %.2f, c_reg %.2f, min_size %d, sigma %.2f, range %d, hie_num %d\n", c, c_reg, min_size, sigma, range, hie_num);

    // count files in the input directory
    int frame_num = 0;
    struct dirent* pDirent;
    DIR* pDir;
    pDir = opendir(input_path);
    if (pDir != NULL) {
        while ((pDirent = readdir(pDir)) != NULL) {
            int len = strlen(pDirent->d_name);
            if (len >= 4) {
                if (strcmp(".ppm", &(pDirent->d_name[len - 4])) == 0)
                    frame_num++;
            }
        }
    }
    if (frame_num == 0) {
        fprintf(stderr, "Unable to find video frames at %s", input_path);
        return 1;
    }
    printf("Total number of frames in fold is %d\n", frame_num);
    // check if the range is right
    if (range > frame_num)
        range = frame_num;
    if (range < 1)
        range = 1;


    // make the output directory
    struct stat st;
    int status = 0;
    char savepath[1024];
    snprintf(savepath,1023,"%s",output_path);
    if (stat(savepath, &st) != 0) {
        /* Directory does not exist */
        if (mkdir(savepath) != 0) {
            status = -1;
        }
    }
    for (int i = 0; i <= hie_num; i++) {
        snprintf(savepath,1023,"%s/%02d",output_path,i);
        if (stat(savepath, &st) != 0) {
            /* Directory does not exist */
            if (mkdir(savepath) != 0) {
                status = -1;
            }
        }
    }
    if (status == -1) {
        fprintf(stderr,"Unable to create the output directories at %s",output_path);
        return 1;
    }

    // Initialize Parameters
    int last_clip = frame_num % range;
    int num_clip = frame_num / range;
    char filepath[1024];
    universe** u = new universe*[num_clip + 1];
    image<rgb>** input_first = new image<rgb>*[range];
    image<rgb>** input_middle = new image<rgb>*[range + 1];
    image<rgb>** input_last = new image<rgb>*[last_clip + 1];

    // Time Recorder
    time_t Start_t, End_t;
    int time_task;
    Start_t = time(NULL);

    // clip 1
    printf("processing subsequence -- 0\n");
    for (int j = 0; j < range; j++) {
        snprintf(filepath, 1023, "%s/%05d.ppm", input_path, j + 1);
        input_first[j] = loadPPM(filepath);
        printf("load --> %s\n", filepath);
    }
    // frame index starts from 0
    u[0] = segment_image(output_path, input_first, 0, range - 1, c, c_reg, min_size,
                         sigma, hie_num, NULL);
    for (int j = 0; j < range; j++) {
        delete input_first[j];
    }

    // clip 2 -- last
    for (int i = 1; i < num_clip; i++) {
        printf("processing subsequence -- %d\n", i);
        for (int j = 0; j < range + 1; j++) {
            snprintf(filepath, 1023, "%s/%05d.ppm", input_path, i * range + j);
            input_middle[j] = loadPPM(filepath);
            printf("load --> %s\n", filepath);
        }
        u[i] = segment_image(output_path, input_middle, i * range - 1,
                             i * range + range - 1, c, c_reg, min_size, sigma, hie_num,
                             u[i - 1]);
        delete u[i - 1];
        for (int j = 0; j < range + 1; j++) {
            delete input_middle[j];
        }
    }

    // clip last
    if (last_clip > 0) {
        printf("processing subsequence -- %d\n", num_clip);
        for (int j = 0; j < last_clip + 1; j++) {
            snprintf(filepath, 1023, "%s/%05d.ppm", input_path, num_clip * range + j);
            input_last[j] = loadPPM(filepath);
            printf("load --> %s\n", filepath);
        }
        u[num_clip] = segment_image(output_path, input_last, num_clip * range - 1,
                                    num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,
                                    hie_num, u[num_clip - 1]);
        delete u[num_clip - 1];
        delete u[num_clip];
        for (int j = 0; j < last_clip + 1; j++) {
            delete input_last[j];
        }
    }

    delete[] u;

    // Time Recorder
    End_t = time(NULL);
    time_task = difftime(End_t, Start_t);
    std::ofstream myfile;
    char timefile[1024];
    snprintf(timefile, 1023, "%s/%s", output_path, "time.txt");
    myfile.open(timefile);
    myfile << time_task << endl;
    myfile.close();

    printf("Time_total = %d seconds\n", time_task);
    return 0;
}
Exemple #26
0
int main(int argc, char** argv) {
	if (argc != 11) {
		printf("%s c c_reg min sigma hie_num start_fr end_fr input output\n", argv[0]);
		printf("       c --> value for the threshold function in over-segmentation\n");
		printf("   c_reg --> value for the threshold function in hierarchical region segmentation\n");
		printf("     min --> enforced minimum supervoxel size\n");
		printf("   sigma --> variance of the Gaussian smoothing.\n");
		printf("   range --> number of frames as one subsequence (k in the paper)\n");
		printf(" hie_num --> desired number of hierarchy levels\n");
		printf(" start_fr --> starting frame index\n");
		printf(" end_fr --> end frame index\n");
		printf("   input --> input path of ppm video frames\n");
		printf("  output --> output path of segmentation results\n");
		return 1;
	}

	// Read Parameters
	float c = (float)atof(argv[1]);
	float c_reg = (float)atof(argv[2]);
	int min_size = atoi(argv[3]);
	float sigma = (float)atof(argv[4]);
	int range = atoi(argv[5]);
	int hie_num = atoi(argv[6]);
	int start_frame_num = atoi(argv[7]);
	int end_frame_num = atoi(argv[8]);
	char* input_path = argv[9];
	char* output_path = argv[10];
	if (c <= 0 || c_reg < 0 || min_size < 0 || sigma < 0 || hie_num < 0) {
		fprintf(stderr, "Unable to use the input parameters.");
		return 1;
	}

	//subarna: optical flow
	double alpha = 0.012;
	double ratio = 0.75;
	int minWidth = 20;
	int nOuterFPIterations = 7;
	int nInnerFPIterations = 1;
	int nSORIterations = 30;

	// count files in the input directory
	/*int frame_num = 0;
	struct dirent* pDirent;
	DIR* pDir;
	pDir = opendir(input_path);
	if (pDir != NULL) {
		while ((pDirent = readdir(pDir)) != NULL) {
			int len = strlen(pDirent->d_name);
			if (len >= 4) {
				if (strcmp(".ppm", &(pDirent->d_name[len - 4])) == 0)
					frame_num++;
			}
		}
	}
	//http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200%28v=vs.85%29.aspx
	if (frame_num == 0) {
		fprintf(stderr, "Unable to find video frames at %s", input_path);
		return 1;
	}
	printf("Total number of frames in fold is %d\n", frame_num);

	// check if the range is right
	if (range > frame_num)
		range = frame_num;
	if (range < 1)
		range = 1;
	*/
	
	// check if the range is right
	if (range > (end_frame_num - start_frame_num))
		range = (end_frame_num - start_frame_num);
	if (range < 1)
		range = 1;

# ifdef USE_OPTICAL_FLOW
	if (range < 2)
	{
		range = 2;
		printf("range value should at least be 2 if motion values are to be used\n");
		printf("making the range value 2");
		if ( (end_frame_num - start_frame_num) < 2 )
		{
			printf("\n Not enough frames ... exiting\n\n\n\n");
			exit(1);
		}
	}
# endif

	// make the output directory
	struct stat st;
	int status = 0;
	char savepath[1024];
  	sprintf(savepath, "%s",output_path);
	if (stat(savepath, &st) != 0) {
		/* Directory does not exist */
		if (mkdir(savepath/*, S_IRWXU*/) != 0) {
			status = -1;
		}
	}
	for (int i = 0; i <= hie_num; i++) {
  		sprintf(savepath, "%s/%02d",output_path,i);
		if (stat(savepath, &st) != 0) {
			/* Directory does not exist */
			if (_mkdir(savepath/*, S_IRWXU*/) != 0) { //subarna: _mkdir
				status = -1;
			}
		}
	}
	if (status == -1) {
		fprintf(stderr,"Unable to create the output directories at %s",output_path);
		return 1;
	}
 
	// Initialize Parameters
	int last_clip = (end_frame_num - start_frame_num + 1) % range;
	int num_clip = (end_frame_num - start_frame_num + 1) / range;
	char filepath[1024];
	universe** u = new universe*[num_clip + 1];

	image<rgb>** input_first = new image<rgb>*[range];
	image<rgb>** input_middle = new image<rgb>*[range + 1];
	image<rgb>** input_last = new image<rgb>*[last_clip + 1];

	//subarna
# ifdef LUV
	DImage** input_first_LUV = new DImage*[range];
	DImage** input_middle_LUV = new DImage*[range+1];
	DImage** input_last_LUV = new DImage*[last_clip+1];
# endif

	// Time Recorder
	time_t Start_t, End_t;
	int time_task;
	Start_t = time(NULL);

	int height, width;
	
	// clip 1
	//calculate pair-wise optical flow
	//initialize memory and first frame
	printf("processing subsequence -- 0\n");
	for (int j = 0; j < range; j++) {
		sprintf(filepath, "%s/%05d.ppm", input_path, j + 1+ start_frame_num);
		input_first[j] = loadPPM(filepath);
		
		printf("load --> %s\n", filepath);

		if (j == 0 )
		{
			height = input_first[0]->height();
			width = input_first[0]->width();
		}

# ifdef LUV
		input_first_LUV[j] = new DImage (width, height,3);
		int m = 0;
		//convert to LUV and then apply bilateral filter
		for (int s = 0; s < height*width; s++)
		{     
			double x1,y1,z1;
			double x2,y2,z2;
			ccRGBtoXYZ(input_first[j]->data[s].r, input_first[j]->data[s].g, input_first[j]->data[s].b, &x1, &y1, &z1);
			ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
			input_first_LUV[j]->pData[m] = x2, 
			input_first_LUV[j]->pData[m+1] = y2, 
			input_first_LUV[j]->pData[m+2] = z2;
			m = m+3;
		}
		// pass input_first_LUV
# endif

	}

#ifdef USE_OPTICAL_FLOW
	DImage** D_vx_motion_first = new DImage*[range];
	DImage** D_vx_motion_middle = new DImage*[range + 1];
	DImage** D_vx_motion_last = new DImage*[last_clip + 1];

	DImage** D_vy_motion_first = new DImage*[range];
	DImage** D_vy_motion_middle = new DImage*[range + 1];
	DImage** D_vy_motion_last = new DImage*[last_clip + 1];
# endif

# ifdef USE_OPTICAL_FLOW
	DImage** D_input_first = new DImage*[range];
	DImage** D_input_middle = new DImage*[range + 1];
	DImage** D_input_last = new DImage*[last_clip + 1];

	for (int i = 0; i < range; i++ )
	{
		D_input_first[i] = new DImage(width,height,3);
		D_vx_motion_first[i] = new DImage (width,height);
		D_vy_motion_first[i] = new DImage (width,height);
	}
	image<rgb>* motion_buffer = NULL;
	DImage* D_motion_buffer = new DImage(width,height,3);

	//initialize
	int m = 0;
	int m1 = 0;
	for(int k1= 0; k1 < width; k1++)
	{
		for (int k2 = 0; k2 < height; k2++)
		{
			D_input_first[0]->pData[m1] = input_first[0]->data[m].r;
			D_input_first[0]->pData[m1+1] = input_first[0]->data[m].g;
			D_input_first[0]->pData[m1+2] = input_first[0]->data[m].b;
			m++;
			m1=m1+3;
		}
	}
	D_input_first[0]->im2double();

	for (int j = 0; j < range-1; j++) {
		// initialization for consecutive frames
		m = 0;	
		m1 = 0;
		for(int k1= 0; k1 < width; k1++)
		{
			for (int k2 = 0; k2 < height; k2++)
			{
				D_input_first[j+1]->pData[m1] = input_first[j+1]->data[m].r;
				D_input_first[j+1]->pData[m1+1] = input_first[j+1]->data[m].g;
				D_input_first[j+1]->pData[m1+2] = input_first[j+1]->data[m].b;
				m++;
				m1=m1+3;
			}
		}
		D_input_first[j+1]->im2double();

		OpticalFlow::Coarse2FineTwoFrames(*D_input_first[j], *D_input_first[j+1], &D_vx_motion_first[j], &D_vy_motion_first[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
		
# ifdef DUMP_FLOW
		//debug: to be deleted
		image<rgb>* test_out = new image<rgb>(width, height);
		int m = 0;	
		for(int k1= 0; k1 < width; k1++)
		{
			for (int k2 = 0; k2 < height; k2++)
			{
				//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
				//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
				//test_out->data[m].b = 0 ; //test_out->data[m].r;

				test_out->data[m].r = 50 + 30*((int)(fabs(D_vx_motion_first[j]->pData[m])));
				test_out->data[m].g = 50 + 30*((int)(fabs(D_vy_motion_first[j]->pData[m])));
				test_out->data[m].b = 0 ; //test_out->data[m].r;
				
				m++;
			}
		}
		sprintf(filepath,"%s/motion/%05d.ppm",output_path, j + 1+ start_frame_num);
		savePPM(test_out, filepath); //"out_test/test1.ppm"
		delete test_out;
# endif

	}
	delete input_first[0];
	sprintf(filepath, "%s/%05d.ppm", input_path, range +1 + start_frame_num);
	input_first[0] = loadPPM(filepath);
	m = 0;	
	m1 = 0;
	for(int k1= 0; k1 < width; k1++)
	{
		for (int k2 = 0; k2 < height; k2++)
		{
			D_motion_buffer->pData[m1] = input_first[0]->data[m].r;
			D_motion_buffer->pData[m1+1] = input_first[0]->data[m].g;
			D_motion_buffer->pData[m1+2] = input_first[0]->data[m].b;
			m++;
			m1=m1+3;
		}
	}
	// dummy place holder
	D_motion_buffer->im2double();
	OpticalFlow::Coarse2FineTwoFrames(*D_input_first[range-2], *D_motion_buffer, &D_vx_motion_first[range-1], &D_vy_motion_first[range-1], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
	
# if 0
	//copy content from D_motion_first[j-1] to D_motion_first[range-1]
	D_vx_motion_first[range-1]->copyData(*D_vx_motion_first[range-2]);
	D_vy_motion_first[range-1]->copyData(*D_vy_motion_first[range-2]);
# endif
	///////////////////////////////////////
# endif

# ifdef USE_OPTICAL_FLOW
	// frame index starts from 0
	# ifndef LUV
		u[0] = segment_image(output_path, input_first, D_vx_motion_first, D_vy_motion_first, 0, range - 1, c, c_reg, min_size,
				sigma, hie_num, NULL,start_frame_num);
	# else
		u[0] = segment_image_LUV(output_path, input_first_LUV, D_vx_motion_first, D_vy_motion_first, 0, range - 1, c, c_reg, min_size,
				sigma, hie_num, NULL,start_frame_num);
	# endif
# else
	// frame index starts from 0
	# ifndef LUV
		u[0] = segment_image(output_path, input_first,0, range - 1, c, c_reg, min_size,
				sigma, hie_num, NULL,start_frame_num);
	# else
		u[0] = segment_image_LUV(output_path, input_first_LUV, 0, range - 1, c, c_reg, min_size,
				sigma, hie_num, NULL,start_frame_num);
	# endif
# endif

	for (int j = 0; j < range; j++) {
		delete input_first[j];
# ifdef LUV
		delete input_first_LUV[j];
# endif
	}

# ifdef USE_OPTICAL_FLOW  //subarna
	for (int j = 0; j < range; j++) {	

		delete D_vx_motion_first[j];
		delete D_vy_motion_first[j];

		delete D_input_first[j];
	}
# endif

	// clip 2 -- last
	int ii;
	for (ii = 1; ii < num_clip; ii++) 
	{
		printf("processing subsequence -- %d\n", ii);

		for (int j = 0; j < range + 1; j++) 
		{
			sprintf(filepath, "%s/%05d.ppm", input_path, ii * range + j + start_frame_num);
			input_middle[j] = loadPPM(filepath);
			
			printf("load --> %s\n", filepath);

# ifdef LUV
			input_middle_LUV[j] = new DImage (width, height,3);
			int m = 0;
			//convert to LUV and then apply bilateral filter
			for (int s = 0; s < height*width; s++)
			{     
				double x1,y1,z1;
				double x2,y2,z2;
				ccRGBtoXYZ(input_middle[j]->data[s].r, input_middle[j]->data[s].g, input_middle[j]->data[s].b, &x1, &y1, &z1);
				ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
				input_middle_LUV[j]->pData[m] = x2, 
				input_middle_LUV[j]->pData[m+1] = y2, 
				input_middle_LUV[j]->pData[m+2] = z2;
				m = m+3;
			}
			//pass input_middle_LUV
# endif
		}

# ifdef USE_OPTICAL_FLOW
		for (int i = 0; i < range+1; i++ )
		{
			D_input_middle[i] = new DImage(width,height,3);
			D_vx_motion_middle[i] = new DImage(width,height);
			D_vy_motion_middle[i] = new DImage(width,height);
		}
		//initialize 
		m = 0;
		m1 = 0;
		for(int k1= 0; k1 < width; k1++)
		{
			for (int k2 = 0; k2 < height; k2++)
			{
				D_input_middle[0]->pData[m1] = input_middle[0]->data[m].r;
				D_input_middle[0]->pData[m1+1] = input_middle[0]->data[m].g;
				D_input_middle[0]->pData[m1+2] = input_middle[0]->data[m].b;
				m++;
				m1=m1+3;
			}
		}
		D_input_middle[0]->im2double();
		//calculate pair-wise optical flow 
		for (int j = 0; j < range; j++) {
			// initialization for consecutive frames
			m = 0;	
			m1 = 0;
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					D_input_middle[j+1]->pData[m1] = input_middle[j+1]->data[m].r;
					D_input_middle[j+1]->pData[m1+1] = input_middle[j+1]->data[m].g;
					D_input_middle[j+1]->pData[m1+2] = input_middle[j+1]->data[m].b;
					m++;
					m1 =m1+3;
				}
			}
			D_input_middle[j+1]->im2double();
			OpticalFlow::Coarse2FineTwoFrames(*D_input_middle[j], *D_input_middle[j+1], &D_vx_motion_middle[j], &D_vy_motion_middle[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
		
# ifdef DUMP_FLOW
			//debug: to be deleted
			image<rgb>* test_out = new image<rgb>(width, height);
			int m = 0;	
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
					//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
					//test_out->data[m].b = 0 ; //test_out->data[m].r;

					test_out->data[m].r = 50 + 40*((int)(fabs(D_vx_motion_middle[j]->pData[m])));
					test_out->data[m].g = 50 + 40*((int)(fabs(D_vx_motion_middle[j]->pData[m])));
					test_out->data[m].b = 0 ; //test_out->data[m].r;
				
					m++;
				}
			}
			sprintf(filepath,"%s/motion/%05d.ppm",output_path, i * range + j + start_frame_num);
			savePPM(test_out, filepath); //"out_test/test1.ppm"
			delete test_out;
# endif

		}
				
		// subarna: fix crash for specific frame number case
		if ( (ii == num_clip - 1 ) && (last_clip == 0) )
		{
			// for the last frame motion feature -- copy feature value from last frame, but with opposite sign
			for (int i = 0; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					D_vx_motion_middle[range]->pData[i*width + j] = -D_vx_motion_middle[range-1]->pData[i*width + j];
					D_vy_motion_middle[range]->pData[i*width + j] = -D_vy_motion_middle[range-1]->pData[i*width + j];
				}
			}
		}
		else
		{
			delete input_middle[0];
			sprintf(filepath, "%s/%05d.ppm", input_path, (ii+1) * range +1 + start_frame_num);
			input_middle[0] = loadPPM(filepath);		

			m = 0;	
			m1 = 0;
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					D_motion_buffer->pData[m1] = input_middle[0]->data[m].r;
					D_motion_buffer->pData[m1+1] = input_middle[0]->data[m].g;
					D_motion_buffer->pData[m1+2] = input_middle[0]->data[m].b;
					m++;
					m1=m1+3;
				}
			}
			
			// dummy place holder
			D_motion_buffer->im2double();
			OpticalFlow::Coarse2FineTwoFrames(*D_input_middle[range], *D_motion_buffer, &D_vx_motion_middle[range], &D_vy_motion_middle[range], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
			/////////////////////////////////////
		}
# endif


# ifdef USE_OPTICAL_FLOW
	# ifndef LUV
		u[ii] = segment_image(output_path, input_middle, D_vx_motion_middle, D_vy_motion_middle, ii * range - 1,
				ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
				u[ii - 1], start_frame_num);
	# else
			u[ii] = segment_image_LUV(output_path, input_middle_LUV, D_vx_motion_middle, D_vy_motion_middle, ii * range - 1,
					ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
					u[ii - 1],start_frame_num);
	# endif
# else
	# ifndef LUV
		u[ii] = segment_image(output_path, input_middle, ii * range - 1,
				ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
				u[ii - 1], start_frame_num);
	# else
			u[ii] = segment_image_LUV(output_path, input_middle_LUV, ii * range - 1,
					ii* range + range - 1, c, c_reg, min_size, sigma, hie_num,
					u[ii - 1],start_frame_num);
	# endif
# endif

		if ( u[ii-1] ) delete u[ii - 1];

		////////////////////
		for (int j = 0; j < range + 1; j++) 
		{
			delete input_middle[j];			
# ifdef LUV
			delete input_middle_LUV[j];
# endif
		}

		# ifdef USE_OPTICAL_FLOW
		for (int j = 0; j < range + 1; j++) 
		{

			delete D_vx_motion_middle[j];
			delete D_vy_motion_middle[j];
			delete D_input_middle[j];
		}
		# endif
	}

	// clip last
	if (last_clip > 0) {
		printf("processing subsequence -- %d\n", num_clip);

		for (int j = 0; j < last_clip + 1; j++) 
		{
			sprintf(filepath, "%s/%05d.ppm", input_path, num_clip * range + j + start_frame_num);

			input_last[j] = loadPPM(filepath);

			printf("load --> %s\n", filepath);

# ifdef LUV
			input_last_LUV[j] = new DImage (width, height,3);
			int m=0;
			for (int s = 0; s < width*height; s++)
			{     
				double x1,y1,z1;
				double x2,y2,z2;
				ccRGBtoXYZ(input_last[j]->data[s].r, input_last[j]->data[s].g, input_last[j]->data[s].b, &x1, &y1, &z1);
				ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
				input_last_LUV[j]->pData[m] = x2, 
				input_last_LUV[j]->pData[m+1] = y2, 
				input_last_LUV[j]->pData[m+2] = z2;
				m = m+3;
			}
# endif
		}

# ifdef USE_OPTICAL_FLOW
		//subarna
		for (int i = 0; i < last_clip+1; i++ )
		{
			D_input_last[i] = new DImage(width,height,3);
			D_vx_motion_last[i] = new DImage(width,height);
			D_vy_motion_last[i] = new DImage(width,height);
		}
		//subarna
		//initialize
		m1 = 0;
		m = 0;
		for(int k1= 0; k1 < width; k1++)
		{
			for (int k2 = 0; k2 < height; k2++)
			{
				D_input_last[0]->pData[m1] = input_last[0]->data[m].r;
				D_input_last[0]->pData[m1+1] = input_last[0]->data[m].g;
				D_input_last[0]->pData[m1+2] = input_last[0]->data[m].b;//0
				m++;
				m1=m1+3;
			}
		}
		D_input_last[0]->im2double();
		//calculate pair-wise optical flow 
		for (int j = 0; j < last_clip; j++) {
			// initialization for consecutive frames
			m = 0;
			m1 = 0;
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					D_input_last[j+1]->pData[m1] = input_last[j+1]->data[m].r;
					D_input_last[j+1]->pData[m1+1] = input_last[j+1]->data[m].g;
					D_input_last[j+1]->pData[m1+2] = input_last[j+1]->data[m].b;
					m++;
					m1=m1+3;
				}
			}
			D_input_last[j+1]->im2double();
			OpticalFlow::Coarse2FineTwoFrames(*D_input_last[j], *D_input_last[j+1], &D_vx_motion_last[j], &D_vy_motion_last[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);

# ifdef DUMP_FLOW
			//debug: to be deleted
			image<rgb>* test_out = new image<rgb>(width, height);
			int m = 0;	
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
					//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
					//test_out->data[m].b = 0 ; //test_out->data[m].r;

					test_out->data[m].r = 50 + 30*((int)(fabs(D_vx_motion_last[j]->pData[m])));
					test_out->data[m].g = 50 + 30*((int)(fabs(D_vx_motion_last[j]->pData[m])));
					test_out->data[m].b = 0 ; //test_out->data[m].r;
				
					m++;
				}
			}
			sprintf(filepath,"%s/motion/%05d.ppm",output_path, num_clip * range + j + start_frame_num);
			savePPM(test_out, filepath); //"out_test/test1.ppm"
			delete test_out;
# endif
		}

		// for the last frame motion feature -- copy feature value from last frame, but with opposite sign
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				D_vx_motion_last[last_clip]->pData[i*width + j] = -D_vx_motion_last[last_clip-1]->pData[i*width + j];
				D_vy_motion_last[last_clip]->pData[i*width + j] = -D_vy_motion_last[last_clip-1]->pData[i*width + j];
			}
		}
		//////////////////////////////////////
# endif

# ifdef USE_OPTICAL_FLOW
	# ifndef LUV
			u[num_clip] = segment_image(output_path, input_last, D_vx_motion_last,D_vy_motion_last, num_clip * range - 1,
					num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,
					hie_num, u[num_clip - 1],start_frame_num);
	# else
			u[num_clip] = segment_image_LUV(output_path, input_last_LUV, D_vx_motion_last,D_vy_motion_last, num_clip * range - 1,
					num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,hie_num, u[num_clip - 1],start_frame_num);
	# endif
# else
	# ifndef LUV
			u[num_clip] = segment_image(output_path, input_last,num_clip * range - 1,
					num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,
					hie_num, u[num_clip - 1],start_frame_num);
	# else
			u[num_clip] = segment_image_LUV(output_path, input_last_LUV, num_clip * range - 1,
					num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,hie_num, u[num_clip - 1],start_frame_num);
	# endif
# endif


		if (u[num_clip - 1]) delete u[num_clip - 1];
		delete u[num_clip];

		for (int j = 0; j < last_clip + 1; j++) 
		{
			delete input_last[j];			
# ifdef LUV
			delete input_last_LUV[j];
# endif
		}

		//subarna
# ifdef USE_OPTICAL_FLOW
		for (int j = 0; j < last_clip + 1; j++) {

			delete D_vx_motion_last[j];
			delete D_vy_motion_last[j];
			delete D_input_last[j];
		}
		delete(D_motion_buffer);
# endif
	}

	//////////////////////////////////////
# ifdef LUV
	delete input_first_LUV;
	delete input_middle_LUV;
	delete input_last_LUV;
# endif

# ifdef USE_OPTICAL_FLOW
	delete D_vx_motion_first;
	delete D_vy_motion_first;
	delete D_vx_motion_middle;
	delete D_vy_motion_middle;
	delete D_vx_motion_last;
	delete D_vy_motion_last;
# endif
	/////////////////////////////////////

	delete[] u;

	// Time Recorder
	End_t = time(NULL);
	time_task = difftime(End_t, Start_t);
	std::ofstream myfile;
	char timefile[1024];
	sprintf(timefile, "%s/%s", output_path, "time.txt");
	myfile.open(timefile);
	myfile << time_task << endl;
	myfile.close();

	printf("Congratulations! It's done!\n");
	printf("Time_total = %d seconds\n", time_task);
	return 0;
}
PPMImage::PPMImage(const char *filename) :
image_(NULL)
{
    loadPPM(filename);
}
Exemple #28
0
Image::Image(const std::string& file)
{
    loadPPM(file);
}
Exemple #29
0
int main(int argc, char **argv) {
	if (argc != 10) {
		fprintf(stderr, "usage: %s sigma k min input output doPreprocess(0/1) doPostprocess(0/1) sort(s/c/f) rgb(0/1)\n", argv[0]);		
		fprintf(stderr, "   input: portable pixelmap (uncompressed) \n");
		fprintf(stderr, "   output: portable pixelmap (uncompressed)\n");
		fprintf(stderr, "   doPreprocess: 0 - without preprocess,  1 - with preprocess\n");
		fprintf(stderr, "   sort: s   - std::sort\n");
		fprintf(stderr, "         csf - parallel counting sort (round)\n");
		fprintf(stderr, "         csr - parallel counting sort (floor)\n");
		fprintf(stderr, "   rgb: 1 - RGB image, 0 - Gray-level (8 bit) images.\n");

		fprintf(stderr, "example: 0.5 500 20 dragon.ppm dragonOut.ppm 1 1 csr 1 \n");


		return 1;

		//example: 0.5 500 20 d:\testImgs\001_test.ppm d:\out001.ppm 1 1 csr 1 
	}

	EtTimer tmr;

	float sigma = (float)atof(argv[1]);
	float c = (float)atof(argv[2]);
	int minSize = atoi(argv[3]);
	bool preProcess = argv[6][0] == '1';
	bool postProcess = argv[7][0] == '1';
	bool rgbProcess = argv[9][0] == '1';

	image<rgb> *input = loadPPM(argv[4]);
	image<rgb> *res = new image<rgb>( input->width(), input->height());


	if( rgbProcess ){
		tmr.start();

		EtGcSegmentRgb* segmenter = new EtGcSegmentRgb(input->width(), input->height(), preProcess, postProcess, 3 );

		tmr.stop();
		std::cout << input->width() << " " << input->height() << " Inicialization " << tmr.getElapsedTime() << std::endl;

		tmr.start();

		segmenter->segment(input, res, sigma, c, minSize, argv[8]); 

		tmr.stop();
		std::cout << res->width() << " " << res->height() << " Full " << tmr.getElapsedTime() << std::endl;

		savePPM(res, argv[5]);
	}
	else{
		tmr.start();
		EtGcSegmentGray* segmenterG = new EtGcSegmentGray(input->width(), input->height(), preProcess, postProcess, 3 );
		image<uchar>* gray = imageRGBtoGRAY(input);

		tmr.stop();
		std::cout << input->width() << " " << input->height() << " Inicialization " << tmr.getElapsedTime() << std::endl;

		tmr.start();

		segmenterG->segment(gray, res, sigma, c, minSize, argv[8]); 

		tmr.stop();
		std::cout << res->width() << " " << res->height() << " Full " << tmr.getElapsedTime() << std::endl;

		savePPM(res, argv[5]);

	}

	return 0;
}
Exemple #30
0
Cube::Cube()
{
    this->toWorld = glm::mat4(1.0f);
    
    this->angle = 0.0f;
    
//    this->toWorld = glm::scale(toWorld, glm::vec3(500.0f, 500.0f, 500.0f));
    
    // Create buffers/arrays
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);
    
    // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
    glBindVertexArray(VAO);
    
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    
    
    glVertexAttribPointer(0,// This first parameter x should be the same as the number passed into the line "layout (location = x)" in the vertex shader. In this case, it's 0. Valid values are 0 to GL_MAX_UNIFORM_LOCATIONS.
                          3, // This second line tells us how any components there are per vertex. In this case, it's 3 (we have an x, y, and z component)
                          GL_FLOAT, // What type these components are
                          GL_FALSE, // GL_TRUE means the values should be normalized. GL_FALSE means they shouldn't
                          3 * sizeof(GLfloat), // Offset between consecutive vertex attributes. Since each of our vertices have 3 floats, they should have the size of 3 floats in between
                          (GLvoid*)0); // Offset of the first vertex's component. In our case it's 0 since we don't pad the vertices array with anything.
    
    glEnableVertexAttribArray(0);
    
    glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
    
    glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
    
    //Get ready for some texture shit
    glGenTextures(1, &textureID);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
    
    // Make sure no bytes are padded:
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    
    unsigned char * image;
    int width = 512;
    int height = 512;
    std::string path;
    if(Window::khalid){
        path = "/Users/adboom/Downloads/skybox/";
    } else {
        path = "/Users/ahmed.elhosseiny/Documents/_CSE 167/Elhosseiny-Ahmed/CSE-167-Final/CSE-167-Final/167Final/";
    }
    image = loadPPM((path + "skybox_water222_right_php8QD91k.ppm").c_str(), width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM((path + "skybox_water222_left_phpPS32dF.ppm").c_str(), width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM((path + "skybox_water222_top_phpEHO9Du.ppm").c_str(), width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM((path + "skybox_water222_base_phpxbs1i6.ppm").c_str(), width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM((path + "skybox_water222_back_phpcsfkI6.ppm").c_str(), width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    image = loadPPM((path + "skybox_water222_front_phpSoNJNF.ppm").c_str(), width, height);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    
    // Select GL_MODULATE to mix texture with polygon color for shading:
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    
    // Use bilinear interpolation:
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    // Use clamp to edge to hide skybox edges:
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    
    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_BACK);

    
    //We're done, we can unbind the texture
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
    

}