示例#1
0
void loadImages(unsigned char **images, int *height, int *width, char *imageFile, int numberOfRepeats = 1)
{
	Image<unsigned char>* input = loadPGM(imageFile);
	const int HEIGHT = input->height();
	const int WIDTH = input->width();
	const int size = HEIGHT * WIDTH * sizeof(char);

	*images = new unsigned char[numberOfRepeats * HEIGHT * WIDTH];

	for(int i = 0; i < numberOfRepeats; i++)
		memcpy(&(*images)[i * HEIGHT * WIDTH], input->data, size);

	*height = HEIGHT;
	*width = WIDTH;
}
示例#2
0
int main(int argv, char **argc) {  
  if (argv != 4) {
    fprintf(stderr, "usage: %s in(pgm) out(pgm) sigma\n", argc[0]);
    exit(1);
  }

  srand48(time(NULL));
  float sigma = atof(argc[3]);

  image<uchar> *im = loadPGM(argc[1]);
  image<uchar> *out = new image<uchar>(im->width(), im->height());
  for (int y = 0; y < im->height(); y++) {
    for (int x = 0; x < im->width(); x++) {
      double r = gaussian()*sigma;
      double v = imRef(im, x, y);
      imRef(out, x, y) = bound(vlib_round(v + r), 0, 255);
    } 
  }

  savePGM(out, argc[2]);
  return 0;
}
示例#3
0
// Initialize buffers, textures, etc.
void initObjects() {
	// Put a bunch of particles into space
	particle* particleData = (particle*)malloc(sizeof(particle) * NUM_PARTICLES);
	GLuint* particleElements = (GLuint*)malloc(sizeof(GLuint) * NUM_PARTICLES);

	srand(666);
	for(int i = 0; i < NUM_PARTICLES; i++) {
		particleData[i].x = centeredUnitRand() * 1.0f * 1.99f;
		particleData[i].y = centeredUnitRand() * 1.0f * 1.99f;
		particleData[i].z = centeredUnitRand() * 1.0f * 1.99f;
		particleData[i].w = 0.0f;
		particleElements[i] = i;
	}
	for(int i = 0; i < 2; i++) {
		particles.vertexBuffer[i] = makeBO(
			GL_ARRAY_BUFFER,
			particleData,
			sizeof(particle) * NUM_PARTICLES,
			GL_DYNAMIC_DRAW
		);
	}
	particles.elementBuffer = makeBO(
		GL_ELEMENT_ARRAY_BUFFER,
		particleElements,
		sizeof(GLuint) * NUM_PARTICLES,
		GL_STATIC_DRAW
	);

	free(particleData);
	free(particleElements);

	// Prepare a screen quad to render postprocessed things.
	Vector quadData[] = {
		{-1.0f, -1.0f, 0.0f},
		{ 1.0f, -1.0f, 0.0f},
		{ 1.0f,  1.0f, 0.0f},
		{-1.0f,  1.0f, 0.0f}
	};
	GLuint quadElements[] = {0, 1, 3, 1, 2, 3};

	screenQuad.vertexBuffer = makeBO(
		GL_ARRAY_BUFFER,
		quadData,
		sizeof(Vector) * 4,
		GL_STATIC_DRAW
	);
	screenQuad.elementBuffer = makeBO(
		GL_ELEMENT_ARRAY_BUFFER,
		quadElements,
		sizeof(GLuint) * 6,
		GL_STATIC_DRAW
	);

	// Load textures.
	terrain.envTexture = loadTexture("skymap_b.tga");
	terrain.lowTexture = loadTexture("sand.tga");
	terrain.highTexture = loadTexture("stone.tga");

	// Create a VAO and bind it
	glGenVertexArrays(1, &vertexArray);
	glBindVertexArray(vertexArray);

	// Prepare a lot of zero'd data
	particle* zeroData = (particle*)malloc(sizeof(particle) * NUM_PARTICLES);
	cl_int* zeroGrid = (cl_int*)malloc(sizeof(cl_int) * GRID_SIZE * GRID_SIZE * GRID_SIZE);
	for(int i = 0; i < NUM_PARTICLES; i++) {
		zeroData[i].x = 0.0f;
		zeroData[i].y = 0.0f;
		zeroData[i].z = 0.0f;
		zeroData[i].w = 0.0f;
	}
	for(int i = 0; i < GRID_SIZE * GRID_SIZE * GRID_SIZE; i++) {
		zeroGrid[i] = 0;
	}

	// Share some buffers with OpenCL and create some more
	for(int i = 0; i < 2; i++) {
		particles.particleBuffer[i] = sharedBuffer(particles.vertexBuffer[i], CL_MEM_READ_WRITE);
		particles.velocityBuffer[i] = clCreateBuffer(
			clContext(), 
			CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 
			NUM_PARTICLES * sizeof(cl_float) * 4, 
			zeroData, 
			NULL
		);
		particles.gridBuffer[i] = clCreateBuffer(
			clContext(), 
			CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 
			sizeof(cl_int) * GRID_SIZE * GRID_SIZE * GRID_SIZE, 
			zeroGrid, 
			NULL
		);
	}
	particles.dataBuffer = clCreateBuffer(
		clContext(), 
		CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 
		NUM_PARTICLES * sizeof(cl_float) * 4, 
		zeroData, 
		NULL
	);
	particles.offsetBuffer = clCreateBuffer(
		clContext(), 
		CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 
		NUM_PARTICLES * sizeof(cl_int), 
		zeroData, 
		NULL
	);
	particles.gridSizeBuffer = clCreateBuffer(
		clContext(), 
		CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 
		sizeof(cl_int) * GRID_SIZE * GRID_SIZE * GRID_SIZE, 
		zeroGrid, 
		NULL
	);
	for(int i = 0; i < 27; i++) {
		zeroGrid[i] = i;
	}
	particles.cellSelectBuffer = clCreateBuffer(
		clContext(), 
		CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 
		sizeof(cl_int) * 27, 
		zeroGrid, 
		NULL
	);
	free(zeroData);
	free(zeroGrid);

	// Load terrain
	terrain.heightData = loadPGM("grand_canyon.pgm", 4096, 4096);
	particles.terrainBuffer = clCreateBuffer(
		clContext(), 
		CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 
		sizeof(cl_float) * 4096 * 4096, 
		terrain.heightData, 
		NULL
	);
	
	// Make terrain texture
	terrain.heightTexture = genFloatTexture(terrain.heightData, 4096, 4096);

	// Make terrain geometry
	PaddedVector* terrainVertices = (PaddedVector*)malloc(sizeof(PaddedVector) * 512 * 512);
	for(int x = 0; x < 4096; x += 8) {
		for(int y = 0; y < 4096; y += 8) {
			int xx = x / 8;
			int yy = y / 8;
			PaddedVector v = PadVector(MakeVector(
				xx * (1.0f / 512.0f) * (AABB_XZ * 2.0f) - AABB_XZ, 
				terrain.heightData[(x / 8) + 4096 * (y / 8)] * 128.0f - 3.0f - 3.0f, 
				yy * (1.0f / 512.0f) * (AABB_XZ * 2.0f) - AABB_XZ
			));
			v.pad = 1.0f;
			terrainVertices[xx + 512 * yy] = v;
		}
	}
	terrain.vertexBuffer = makeBO(
		GL_ARRAY_BUFFER,
		terrainVertices,
		sizeof(PaddedVector) * 512 * 512,
		GL_STATIC_DRAW
	);
	free(terrainVertices);

	GLuint* terrainElements = (GLuint*)malloc(sizeof(GLuint) * 512 * 512 * 6);
	int quadIndex = 0;
	for(int x = 0; x < 511; x++) {
		for(int y = 0; y < 511; y++) {
			terrainElements[quadIndex * 6 + 0] = (x + 0) + (y + 0) * 512;
			terrainElements[quadIndex * 6 + 1] = (x + 1) + (y + 1) * 512;
			terrainElements[quadIndex * 6 + 2] = (x + 1) + (y + 0) * 512;
			terrainElements[quadIndex * 6 + 3] = (x + 0) + (y + 0) * 512;
			terrainElements[quadIndex * 6 + 4] = (x + 0) + (y + 1) * 512;
			terrainElements[quadIndex * 6 + 5] = (x + 1) + (y + 1) * 512;
			quadIndex++;
		}
	}
	terrain.elementBuffer = makeBO(
		GL_ELEMENT_ARRAY_BUFFER,
		terrainElements,
		sizeof(GLuint) * 512 * 512 * 6,
		GL_STATIC_DRAW
	);

	free(terrainElements);
}
示例#4
0
int main(int argc, char *argv[]) {
    imagePGM srcImg;
    imagePGM dstImg;
    char *srcFilename;
    char *dstFilename;
    int FileLoad = 0, FileLoadFilename = 0, FileSave = 0, FileSaveFilename = 0, flagInfo = 0, FileInfoOptions = 0;
    int FileImage = 0, FileImageOptions = 0, rotAngle = 0;
    int c;
    int index;

    while ((c = getopt(argc, argv, "i:o:hnr:")) != -1) {
        switch (c) {
            case 'h':
                printHelp();
                exit(EXIT_SUCCESS);
            case 'n':
                flagInfo = 1;
                break;
            case 'i':
                FileLoad = 1;
                FileLoadFilename = 1;
                srcFilename = optarg;
                break;
            case 'o':
                FileSave = 1;
                FileSaveFilename = 1;
                dstFilename = optarg;
                break;
            case 'r':
                FileImage = 1;
                FileImageOptions = 1;
                rotAngle = atoi(optarg);
                break;
            case '?':
                break;
                /*
                if (optopt == 'i')
                    fprintf(stderr, "Option -%c requires an argument.\n", optopt);
                else if (optopt == 'o')
                    fprintf(stderr, "Option -%c requires an argument.\n", optopt);
                else if (optopt == 'r')
                    fprintf(stderr, "Option -%c requires an argument.\n", optopt);
                else if (isprint(optopt))
                    fprintf(stderr, "Unknown option `-%c'.\n", optopt);
                else
                    fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
                return 1;
                */
            default:
                fprintf(stderr, "Wrong arguments. try simpleocr -h for help.");
                exit(EXIT_FAILURE);
        }
    }
    
    ///printf("src = %s\n", dstFilename);
    
    if (FileLoad && FileLoadFilename) {
        srcImg = loadPGM(srcFilename);
        if (flagInfo) {
            printf ("Properties:\n");
            printf ("format: %s\n", getFormatPGM(srcImg));
            printf ("depth: %d\n", getMaxIntensityPGM(srcImg));
            printf ("width: %d\n", getColsPGM(srcImg));
            printf ("height: %d\n", getRowsPGM(srcImg));
            printf ("###: %d\n", getRowsPGM(srcImg));
            dstImg = buildHistogram(srcImg);
            savePGM("histogram.pgm", dstImg);
        }
    }
    
    if (FileSave && FileSaveFilename) {
        if (FileImage) {
            dstImg = rotatePGM(srcImg, rotAngle);
        }
        savePGM(dstFilename, dstImg);
        freePGM(dstImg);
        printf ("Done\n");
    }
    
    return 0;
}