I'm currently trying to do a black and white filter for my game in c++ with Allegro 5.01, I'm doing like this.
void GameEngine::blackAndWhite()
{
ALLEGRO_BITMAP *buffer = al_get_backbuffer(this->windows);
ALLEGRO_LOCKED_REGION *region = al_lock_bitmap(buffer, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_READWRITE);
unsigned char *ptr = static_cast<unsigned char *>(region->data);
for (int x = 0; x != 640; ++x) {
for (int y = 0; y != 480; ++y) {
unsigned char red;
unsigned char green;
unsigned char blue;
this->getpixel(ptr, x, y, region->pitch, &red, &green, &blue);
unsigned char result = (red + green + blue) / 3;
this->putpixel(ptr, x, y, region->pitch, al_map_rgb(result, result, result));
}
}
al_unlock_bitmap(buffer);
}
And putpixel and getpixel is like this.
void GameEngine::putpixel(unsigned char *ptr, int x, int y, int offset_y, ALLEGRO_COLOR color) {
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
ptr += (y * offset_y) + (x * 4);
al_unmap_rgba(color, &red, &green, &blue, &alpha);
*ptr++ = red;
*ptr++ = green;
*ptr++ = blue;
*ptr = alpha;
}
void GameEngine::getpixel(unsigned char *ptr, int x, int y, int offset_y,
unsigned char *red, unsigned char *green, unsigned char *blue)
{
ptr += (y * offset_y) + (x * 4);
*red = *ptr++;
*green = *ptr++;
*blue = *ptr++;
}
It's working, but it's really slow, I'm thinking it's slow because this operation is do by the CPU and not the GPU.
Is there a way for reduce the time of the operation with allegro or not ?