How would one have a function such as al_autocrop_bitmap() that would get rid of extra space if all pixels are transparent? I think I would use something like
void crop_bmp(BITMAP ** bmp, int ix, int iy, int fx , int fy){ BITMAP * temp; temp = create_bitmap(fx-ix, fy-iy); clear_to_color(temp, MAGENTA); draw_sprite(temp,*bmp,-ix,-iy); destroy_bitmap(*bmp); *bmp = temp; }
found at https://www.allegro.cc/forums/thread/607897
But I want to remove edge line by line from top,bottom, left, and right if it is transparent. How can I check if a pixel is transparent in allegro? Should I draw onto a FrameBuffer and then get pixel from that? Is this the right way of doing this?
I have some old code from a Bitmap triangulator that found the contour of a bitmap by seeking through its transparent pixels:
Basically you lock it.
Then do:
unsigned char *row = (unsigned char*)region->data; row += (int)YOUR_Y_COORDINATE_HERE * region->pitch; uint32_t *pixel = (unsigned int*)row + (int)YOUR_X_COORDINATE_HERE; int alpha = *pixel >> 24;
Are you using A4 or A5? Your example code is A4, but me thinks you are using A5? IDK.
If A4 - getpixel
If A5 - al_lock_bitmap_region
A5
What about using this to check if a pixel is transparent: https://www.allegro.cc/forums/thread/608076/927864 ?
This takes an image, pulls the top left color, and draws a green rectangle showing trim region that is made of up that color.
(You'll use this too):
namespace color { bool basically_equal(const ALLEGRO_COLOR &col1, const ALLEGRO_COLOR &col2, float threshold=0.0001) { if (abs(col1.a - col2.a) > threshold) return false; if (abs(col1.g - col2.g) > threshold) return false; if (abs(col1.b - col2.b) > threshold) return false; if (abs(col1.r - col2.r) > threshold) return false; return true; } }