'Fashion Model' Graphics. lazarus / 39-Graphics ---------------------------------------------------------------------- huwmungous · 2010-03-18 15:54 As kids we used to have a game made from cardboard cutouts. This was a fashion model, which you could 'Dress' with clothing made from further bits of card representing wigs, coats, shoes and so-on. I want to implement a similar thing grapically as a lazarus component.  A collection of images should be overlaid to produce a single unlayered result, with a transparent background. I think ideally I want the ouput to be .png, but could do with some advice on that. There is one small complication; some items need to remove bits of the fashion model as they are 'donned', so some parts of an overlay may be opaque until they are applied and transparent thereafter. I am a reasonable programmer, but normally do business apps, and have (nearly) no experience with graphical stuff.  If anyone can advise me on the general best approach I would be most grateful. ---------------------------------------------------------------------- circular · 2010-03-18 16:07 I have just made a unit that can be helpful in your task. You can use it to load images, merge them, draw and save them. You can also access directly to bitmap data if you need to. See this topic : http://www.lazarus.freepascal.org/index.php/topic,7166.0.html Or get the file directly : http://consume.o2switch.net/lazarus/bgrabitmap.zip Is it useful to you ? ---------------------------------------------------------------------- huwmungous · 2010-03-18 16:47 Thanks for that I will look into it over the next couple of days. The 2 aspects of this I am unclear about are   1) Merging 2 images   2) changing the masking bit of an 'overlay' to transparent colour in the result. what is the method in your unit that handles 1) ? is there a method in your unit that can replace every pixel of a given colour to another colour ? ---------------------------------------------------------------------- circular · 2010-03-18 18:31 You can do 1) for example by creating a bitmap object as destination, and then draw each bitmap with PutImage : Code: [Select]var image1, image2, dest : TBGRABitmap; begin   image1 := TBGRABitmap.Create('image1.png');   image2 := TBGRABitmap.Create('image2.png');   dest := TBGRABitmap.Create(800,600);  //create 800x600 destination bitmap   dest.PutImage(0,0, image1, dmDrawWithTransparency);   dest.PutImage(0,0, image2, dmDrawWithTransparency);   dest.Draw(Canvas, 0,0); //draw bitmap on some window end; I have not done any procedure for 2) but it a small code. For example, if you want to change Red pixels into Blue pixels, you can do it this way : Code: [Select]var n: integer;     p: PBGRAPixel;     before,after: TBGRAPixel; begin    p := image.Data;    before := BGRAPixel(clRed); //opaque red    after := BGRAPixel(clBlue); //opaque blue    for n := image.NbPixels-1 downto 0 do    begin      if p^ = before then p^ := after;      inc(p); //go to next pixel    end; end; ---------------------------------------------------------------------- circular · 2010-03-19 00:31 I've added two functions ReplaceColor. I was wondering how to accelerate the drawing, and there is some solution used in the LazRGBGraphics units : http://wiki.freepascal.org/LazRGBGraphics My unit could be merged with this package, in fact, it does what is written in the TODO list. But the structure is a little bit different. ---------------------------------------------------------------------- huwmungous · 2010-03-22 08:33 Thanks very much for that.  I am not sure I will be able to get some time to have a look at this just yet, but I will report back as soon as I get some results. I notice the unit does not compile under Delphi; there is a number of '+=' and '*=' operations in the code (understood), and a line 'operator = ....' at the top of the unit.  What is that last contruct ? Is it a 'Macro' ? ---------------------------------------------------------------------- Marc · 2010-03-22 09:09 Quote from: huwmungous on March 22, 2010, 09:33:25 am... and a line 'operator = ....' at the top of the unit.  What is that last construct ? Is it a 'Macro' ? No, this isn't a macro, but operator overloading. With operator is ... you can define how an operate should work on custom types. See the freepascal Language Reference Guide: Operator Overloading ---------------------------------------------------------------------- circular · 2010-03-22 20:06 Operator overloading is like functions. In this cas, it allows to check if two colors are identical with the '=' sign. I like the += notation. It comes from the C syntax, but it is safe explanatory. Thanks coders for including it in Free Pascal ! ---------------------------------------------------------------------- huwmungous · 2010-03-23 16:42 yeah - I like '+=', '*=' too. Looks like there is more to this FreePascal than meets the eye...  I started with standard Pascal quite a few years ago and have been using Delphi ever since.  I wonder hwat other nice little suprises there are ...