Skip to content

Instantly share code, notes, and snippets.

@cmichi
Created March 16, 2011 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmichi/873572 to your computer and use it in GitHub Desktop.
Save cmichi/873572 to your computer and use it in GitHub Desktop.
Function which returns the pixel color for a XBM (X BitMap) Image
/*
* returns the pixel color for a XBM (X BitMap) image.
*
* XBM is a plain text monochrome image format that takes the form of
* C source code. images can directly be compiled into an application.
*
*/
static int is_black(int x, int y, int img_width, int img_height)
{
char byte_containing_px;
/* byte, containing pixel bit */
int byte_containing_px_position = ((y * img_width) + x) / 8;
if (img_width % 8 > 0)
byte_containing_px_position += (y / 8) + 1;
/* pointer in ranges? */
if (byte_containing_px_position >= ((img_width * img_height) / 8))
return 0;
byte_containing_px = *(earth_bits + byte_containing_px_position);
/* searched byte has to be on rightmost position */
byte_containing_px >>= x;
return (byte_containing_px & 0x1) ? 1 : 0;
}
/*
* checks whether there is a bigger black then white part
* in the square "width * height"
*/
static Bool is_more_col(int x, int y, float point_width_on_map,
float point_height_on_map)
{
int x1, y1;
int pixels_in_color = 0;
int treshold = (point_width_on_map * point_height_on_map) / 2;
for (x1 = 0; x1 < point_width_on_map; x1++) {
for (y1 = 0; y1 < point_height_on_map; y1++) {
if (is_black(x + x1, y + y1, EARTH_WIDTH, EARTH_HEIGHT) == 1)
pixelsInColor++;
}
}
if (pixels_in_color >= treshold)
return True;
else
return False;
}
@Firleyinc
Copy link

Firleyinc commented Jan 22, 2020

I've found out that this function doesn't work when bitmap is wider than 8 pixels - it happens because of that bit shift in line 24. Program tries to shift byte more than 8 bits right, therefore returns always zero. It can be fixed easily by replacing x with x%8 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment