Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » [A5] al_load_bitmap issue with PNG

This thread is locked; no one can reply to it. rss feed Print
[A5] al_load_bitmap issue with PNG
ngiacomelli
Member #5,114
October 2004

I'm seeing intermittent issues when loading and rendering PNG files via the iPhone port. Occasionally the images will load fine, other times I'll witness graphical distortion. I made a test case. All I need to do is run this a few times via the simulator to see a number of artifacts. Some are shown here:

{"name":"600727","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/0\/b070017ef2663d1ccb5e82ded96f99eb.jpg","w":300,"h":300,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/0\/b070017ef2663d1ccb5e82ded96f99eb"}600727

Here's the code used when seeing the above artifacts. I've also attached the png files in question.

#SelectExpand
1int main(void) 2{ 3 al_init(); 4 5 // Setup display 6 ALLEGRO_DISPLAY *display = al_create_display(480, 320); 7 if (!display) { 8 printf("Error creating display.\n"); 9 return 1; 10 } 11 12 al_init_image_addon(); 13 14 ALLEGRO_TIMER *timer = al_install_timer(1.0 / 60); 15 ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue(); 16 al_register_event_source(queue, al_get_display_event_source(display)); 17 al_register_event_source(queue, al_get_timer_event_source(timer)); 18 al_start_timer(timer); 19 20 ALLEGRO_BITMAP *t1 = al_load_bitmap("walk.png"); 21 ALLEGRO_BITMAP *t2 = al_load_bitmap("run.png"); 22 23 bool needRender = false; 24 25 ALLEGRO_EVENT event; 26 27 while (1) { 28 al_wait_for_event(queue, &event); 29 30 switch(event.type) { 31 case ALLEGRO_EVENT_TIMER: 32 needRender = true; 33 break; 34 case ALLEGRO_EVENT_DISPLAY_CLOSE: 35 break; 36 } 37 38 if (needRender && al_event_queue_is_empty(queue)) { 39 al_draw_bitmap(t1, 0, 0, 0); 40 al_draw_bitmap(t2, 0, 25, 0); 41 al_flip_display(); 42 needRender = false; 43 } 44 } 45 46 return 0; 47}

I'm not sure if this is a genuine issue, or a mistake on my part.

Trent Gamblin
Member #261
April 2000
avatar

Couple questions. Are you using (to your knowledge) the libpng support in the Allegro image addon or the Apple WhateverYouCallIt loader?

Also, I didn't think it was true for the simulator, but it may be, and definitely is on the device... Apple has this brilliant way of compressing pngs in your bundle that makes them incompatible with libpng. So try adding this key value pair to your project settings:

IPHONE_OPTIMIZE_OPTIONS -skip-PNGs

ngiacomelli
Member #5,114
October 2004

To my knowledge, I'm using libpng. I certainly installed it with DarwinPorts before running cmake to compile Allegro. Is there anyway to tell which is being used, or is it a conscious decision on my part?

I also added the -skip-PNGs flag for All Configurations in the Project Settings pane - but this doesn't seem to have helped much.

Trent Gamblin
Member #261
April 2000
avatar

The code you posted looks completely different from the screenshot. I get both images drawn to the top left (if holding in landscape). No garbage. Can you post the code that produces the image above?

ngiacomelli
Member #5,114
October 2004

The image above was the result of me running the code posted above a few times. I've just positioned multiple results in one image for convenience.

As mentioned, occasionally the images will load fine. Other times, I'll get the artifacts shown above.

EDIT: To clarify, the screenshot shows the results of two executions of my test case. In the first set, walk.png is rendered fine, while run.png experiences strange artifacts.

In the second, both walk.png and run.png are distorted.

Trent Gamblin
Member #261
April 2000
avatar

I see. Well I tried it about 10 times with no garbage here... btw I just found out you are probably using the Apple native image loader... that's the default now. I don't have any experience using that, so I can't say how good it is. In my game I'm using like allegro 4.9.16 or something with some patches. But since it works fine here each time I try it, I'm not sure the problem. Do you get any artifacts with any other opengl programs on your mac?

ngiacomelli
Member #5,114
October 2004

Nope. I haven't had any graphic artifacts elsewhere.

Is it possible that the PNG is causing the issue? You mentioned compression/optimization on the iPhone end. Currently, I'm saving out my graphics via Photoshop. Perhaps it's choking on the format?

Trent Gamblin
Member #261
April 2000
avatar

If that were the case I would expect corruption everytime, and on my end too. But it may be that we're using different sdks. I don't think I'm on the very latest or the beta... It wouldn't hurt to try re saving them with Preview or something though, and see if the garbage goes away. It's highly possible there is a bug in Allegro's image loader or locking routines too.

ngiacomelli
Member #5,114
October 2004

Thanks to this thread I managed to fix my rendering issues by clearing the drawing context in the image addon iphone.m file.

It seems like PNGs with transparency were being layered over the top of each other (which explained why bitmaps loaded fine), and this seems to resolve the issue.

   /* Now we need to draw the image into a memory buffer. */
   pixels = _AL_MALLOC(w * h * 4);
   CGContextRef context = CGBitmapContextCreate(pixels, w, h, 8, w * 4,
      CGImageGetColorSpace(uiimage.CGImage),
      kCGImageAlphaPremultipliedLast);
  CGContextSetBlendMode(context, kCGBlendModeCopy); // Clear context
   CGContextDrawImage(context, CGRectMake(0.0, 0.0, (CGFloat)w, (CGFloat)h),
      uiimage.CGImage);
   CGContextRelease(context);

I'm not sure whether this is a solid fix, or if it could help anyone else - but it seems to have worked for me.

Trent Gamblin
Member #261
April 2000
avatar

I've added it to the bug tracker, as I don't know the code well myself. Elias could probably tell if this is a good fix or not.

Edit: Ok, should be fixed in SVN. Thanks for that.

Evert
Member #794
November 2000
avatar

Edit: Ok, should be fixed in SVN. Thanks for that.

The OS X native loader probably needs a similar fix. Did you do that too?
(I could just look at the change log, but I'm feeling lazy).

Trent Gamblin
Member #261
April 2000
avatar

No, I did not.

Evert
Member #794
November 2000
avatar

Ok. Actually looking at the code, it probably doesn't need it. It uses the Cocoa NSImage interface to do the drawing, and it explicitly sets NSCompositeCopy, which should be what we want.

Go to: