Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Cropping a Bitmap (Allegro 5)

This thread is locked; no one can reply to it. rss feed Print
Cropping a Bitmap (Allegro 5)
Henrique Rocha
Member #13,042
July 2011

Hello again, I am having troubles with cropping a bitmap

#define MAGENTA al_map_rgb(255,0,255)

void crop_bmp(ALLEGRO_BITMAP ** bmp, int ix, int iy, int fx , int iy){
ALLEGRO_BITMAP * temp;
ALLEGRO_DISPLAY * display=al_get_current_display();
temp = al_create_bitmap(fx-ix, fy-iy);
al_set_target_bitmap(temp);
al_clear_to_color(MAGENTA);
al_draw_bitmap(*bmp, -ix, -iy, 0);
al_destroy_bitmap(*bmp);
*bmp = temp;
al_set_target_bitmap(al_get_backbuffer(display));
}

this is the error (it's in Portuguese):
era: /home/henriquerocha/allegro-5.0/src/display.c:187: al_clear_to_color: Asserção `target' falhou.

What is wrong? I don't want to create a sub bitmap. Just making a new one but shorter.

Thanks ;)

Arthur Kalliokoski
Second in Command
February 2005
avatar

void crop_bmp(ALLEGRO_BITMAP ** bmp, int ix, int iy, int fx , int iy)

temp = al_create_bitmap(fx-ix, fy-iy)

Where does the second line get fy from?

They all watch too much MSNBC... they get ideas.

Henrique Rocha
Member #13,042
July 2011

sorry It was a mistake when I was writting here,

void crop_bmp(BITMAP ** bmp, int ix, int iy, int fx, int fy)

the error is the same.

Arthur Kalliokoski
Second in Command
February 2005
avatar

For one thing, you don't pass the contents of a bitmap to al_draw_bitmap() and friends, you just pass the pointer.

al_draw_bitmap(*bmp, -ix, -iy, 0);
should be
al_draw_bitmap(bmp, -ix, -iy, 0);

and you're not checking return values

temp = al_create_bitmap(fx-ix, fy-iy);
al_set_target_bitmap(temp);

should be more like

temp = al_create_bitmap(fx-ix, fy-iy);
if(!temp)
{
 put some sort of error message here
 return 1;
}
al_set_target_bitmap(temp);

They all watch too much MSNBC... they get ideas.

SiegeLord
Member #7,827
October 2006
avatar

Perhaps ix is greater than fx (or the same for y) and your al_create_bitmap call is failing?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Henrique Rocha
Member #13,042
July 2011

void crop_bmp(ALLEGRO_BITMAP ** bmp, int ix, int iy, int fx , int iy)

al_draw_bitmap(*bmp, -ix, -iy, 0);
I think this line is correct, bmp is **, the goal is to replace the image for the cropped image, that's why I use double pointer.

The strange is that with allegro 4.2.2 works great.

#SelectExpand
1void crop_bmp(BITMAP ** bmp, int ix, int iy, int fx , int fy){ 2 BITMAP * temp; 3 4 temp = create_bitmap(fx-ix, fy-iy); 5 clear_to_color(temp, MAGENTA); 6 7 draw_sprite(temp,*bmp,-ix,-iy); 8 9 destroy_bitmap(*bmp); 10 *bmp = temp; 11}

But I am trying to convert it for allegro 5.

PS: Sorry problem is not here.

pixel = al_get_pixel(bmp,x,y);
printf("%f.%f.%f\n",pixel.r,pixel.g,pixel.b);

The bitmap is MAGENTA but that printf is always
"0,000000.0,000000.0,000000"

I am using getpixel to calculate ix and fx

PS: Problem solved! Thanks for your support ;)

Go to: