Example #1
0
void Bitmap::equals_naive_blur(Bitmap &original)
{
	allocate_bitmap(original.height, original.width);

	// get the body:
	for(int r = 1; r < height-1; r++)
	{
		for(int c = 1; c < width-1; c++)
		{
			bitmap[r][c].set(0);
			for(int y = -1; y <= 1; y++)
			{
				for(int x = -1; x <= 1; x++)
				{
					bitmap[r][c] += original[r+y][c+x];
				}
			}
			bitmap[r][c] /= 9;
		}
	}

	// get the edges and corners:
	for(int r = 0; r < height; r++)
	{
		int c_skip = (r==0 || r==height-1) ? 1 : width-1;
		for(int c = 0; c < width; c += c_skip)
		{
			bitmap[r][c].set(0);
			int count = 0;
			for(int y = -1; y <= 1; y++)
			{
				for(int x = -1; x <= 1; x++)
				{
					int new_r = r+y, new_c = c+x;
					if(	0 <= new_r && new_r < height &&		// the conditions since we know we're on some edge
						0 <= new_c && new_c < width)
					{
						bitmap[r][c] += original[r+y][c+x];
						count += 1;
					}
				}
			}
			bitmap[r][c] /= count;
		}
	}
}
Example #2
0
int load_pnm_from_FILE(FILE *f, unsigned char ***pixels, int *width, int *height)
{
    int maxval;
    char type;

    if (getc(f) != 'P')
    {
        fprintf(stderr, "PNM file not starts with 'P'\n");
        exit(1);
    }
    
    type = getc(f);
    skip_whitespace_and_comments(f);
    
    switch(type)
    {
        case '4':
            fscanf(f, "%d %d", width, height);
            maxval = 255;
        break;
        case '5': case '6':
            fscanf(f, "%d %d %d", width, height, &maxval);
        break;
        default:
            fprintf(stderr, "only raw PNM files supported\n");
            exit(1);
            
    }

    if (maxval != 255)
    {
        fprintf(stderr, "only 256-levels PNM supported\n");
        exit(1);
    }

    switch(fgetc(f))
    {
        case ' ': case '\t': case '\r': case '\n':
            break;
        default:
            fprintf(stderr, "corrupted PNM, current offset is %ld\n", ftell(f));
            exit(1);
    }

    switch(type)
    {
        case '4':
            *pixels = allocate_bitmap(*width, *height);
            load_pbm_raster(f, *pixels, *width, *height);
            return PBM;
        case '5':
            *pixels = allocate_bitmap(*width, *height);
            fread(**pixels, *width, *height, f);
            return PGM;
        case '6':            
            *pixels = allocate_bitmap(*width * 3, *height);
            fread(**pixels, *width * 3, *height, f);
            return PPM;
        default:
            assert(0);
            return 0;
    }
}