Copy an image

7 posts / 0 new
Last post
mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
Copy an image

I have loaded an image with this

  1. Object *
  2. LoadInternalImage(STRPTR Render)
  3. {
  4. return(IIntuition->NewObject(BitMapClass,NULL,
  5. BITMAP_SourceFile, Render,
  6. BITMAP_Screen, DefaultPubScreen,
  7. BITMAP_Precision, PRECISION_EXACT,
  8. BITMAP_Masking, TRUE,
  9. TAG_DONE));
  10. }

Now I am trying to make a copy of it to keep the original and do some modifying to the copy. Everything I have tried loses the alpha transparency and it shows up pink. The image body is fine.

  1. Object *
  2. CopyImage(Object *SourceImg)
  3. {
  4. Object *Img;
  5. struct BitMap *SourceBM, *DestBM;
  6. uint32 Width, Height, Depth, PixelFormat;
  7.  
  8. if (SourceImg==NULL)
  9. return(NULL);
  10.  
  11. IIntuition->GetAttrs(SourceImg,
  12. BITMAP_BitMap, &SourceBM,
  13. TAG_DONE);
  14.  
  15. Width=IGraphics->GetBitMapAttr(SourceBM,BMA_WIDTH);
  16. Height=IGraphics->GetBitMapAttr(SourceBM,BMA_HEIGHT);
  17. Depth=IGraphics->GetBitMapAttr(SourceBM,BMA_DEPTH);
  18. // Depth=IGraphics->GetBitMapAttr(SourceBM,BMA_BITSPERPIXEL);
  19. PixelFormat=IGraphics->GetBitMapAttr(SourceBM,BMA_PIXELFORMAT);
  20.  
  21. // I am forcing it to this format. Either way has same result.
  22. if (!(DestBM=IGraphics->AllocBitMapTags(Width,Height,32,//Depth,
  23. BMATags_PixelFormat, PIXF_A8R8G8B8,//PixelFormat,
  24. BMATags_Clear, TRUE,
  25. BMATags_Displayable, TRUE,
  26. TAG_DONE)))
  27. {
  28. return(NULL);
  29. }
  30.  
  31. // I have tried every combination, plus others.
  32. if (IGraphics->CompositeTags(
  33. COMPOSITE_Src, SourceBM, DestBM,
  34. // COMPOSITE_Src_Over_Dest, SourceBM, DestBM,
  35. // COMPTAG_Flags, COMPFLAG_IgnoreDestAlpha,
  36. // COMPTAG_Flags, COMPFLAG_SrcAlphaOverride | COMPFLAG_IgnoreDestAlpha,
  37. // COMPTAG_Flags, COMPFLAG_SrcAlphaOverride,
  38. TAG_DONE) != COMPERR_Success)
  39. {
  40. IGraphics->FreeBitMap(DestBM);
  41.  
  42. return(NULL);
  43. }
  44.  
  45. if (!(Img=IIntuition->NewObject(BitMapClass,NULL,
  46. BITMAP_BitMap, DestBM,
  47. BITMAP_Width, Width,
  48. BITMAP_Height, Height,
  49. BITMAP_HasAlpha, TRUE,
  50. BITMAP_Screen, DefaultPubScreen,
  51. BITMAP_Precision, PRECISION_EXACT,
  52. BITMAP_Masking, TRUE,
  53. TAG_DONE)))
  54. {
  55. IGraphics->FreeBitMap(DestBM);
  56.  
  57. return(NULL);
  58. }
  59.  
  60. return(Img);
  61. }

What is happening to the transparency? The original image displays just fine, the copy does not (pink).

broadblues
broadblues's picture
Offline
Last seen: 4 years 10 months ago
Joined: 2012-05-02 21:48
Re: Copy an image

The problem is the probably Composite call. CompoisteTags does a composite operation not a copy. Thus the alpha channel is not preserved, rather it's used to control the composite. A few people have been caught out by this.

It's not obvious to me what specific change you are trying to make with the Composite operation?

mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
Re: Copy an image

That's the weird thing: CompositeTags() does "save" the alpha channel when loaded from a datatype. I use it a lot in my program. I load a BitMapClass with BITMAP_Render,"path", and such; AllocBitMapTags() a new bitmap; lock it and IDoMethod() pdtBlitPixelArrayBuffer to it, unlock it; AllocBitMapTags() another bitmap (destination); CompositeTags() with a scale down; create BitMapClass with destination bitmap and all it good.

I have tried with a mix and match of these tags with no luck, or worse results:

  1. COMPTAG_Flags, COMPFLAG_IgnoreDestAlpha,
  2. COMPTAG_Flags, COMPFLAG_SrcAlphaOverride | COMPFLAG_IgnoreDestAlpha,
  3. COMPTAG_Flags, COMPFLAG_SrcAlphaOverride,

But above I have already loaded the image to a BitMapClass and am copying the BITMAP_BitMap from it and the transparency gets lost; end up violet.

I am using CompositeTags() because I may need to scale it, so just have it ready to go (currently just copying it). I also tried to use BltBitMapTags() but has the same result: alpha is violet.

  1. IGraphics->BltBitMapTags(
  2. BLITA_Source, SourceBM,
  3. BLITA_Dest, DestBM,
  4. BLITA_SrcType, BLITT_BITMAP,
  5. BLITA_DestType, BLITT_BITMAP,
  6. BLITA_Width, Width,
  7. BLITA_Height, Height,
  8. BLITA_UseSrcAlpha, TRUE,
  9. TAG_DONE);

So I would say it has something to do with the way GetAttrs() BITMAP_BitMap is getting the data, losing the alpha data along the way. ??????

broadblues
broadblues's picture
Offline
Last seen: 4 years 10 months ago
Joined: 2012-05-02 21:48
Re: Copy an image

BLITA_UseSrcAlpha, TRUE,

will alpha blend the src with the destination, that *will* lose the alpha component of the src, you want a straight copy with no blending.

But I have to say your approach is wrong. bitmap.image is not for loading images but displaying them (naturally it supports loading too, as a convenience).

If you want to load an image them modify it, use the datatype directly, then extact the 32bit bitmap, modify as required and create your new bitmap.image from it.

mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
Re: Copy an image

I hear what you are saying about using datatypes to load the image, will try that later. For now, I I found another way to do what I need. I still need/want to get the copy to work, not giving up on it.

mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
Re: Copy an image

I used the "traditional" method of loading the image with datatypes and BltBitMapTags(), got success.

hypex
hypex's picture
Offline
Last seen: 4 months 2 weeks ago
Joined: 2011-09-09 16:20
Re: Copy an image

Just out of interest, did you also try copying the bitmap by grabbing a pointer to it and passing that along with BITMAP_BitMap as the source instead? Thus avoiding all the duplicating you were doing. If that could work.

Log in or register to post comments