Recent

Author Topic: 'Fashion Model' Graphics.  (Read 9677 times)

huwmungous

  • New Member
  • *
  • Posts: 14
'Fashion Model' Graphics.
« on: March 18, 2010, 04:54:09 pm »
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

  • Hero Member
  • *****
  • Posts: 2972
    • Personal webpage
Re: 'Fashion Model' Graphics.
« Reply #1 on: March 18, 2010, 05:07:01 pm »
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 ?
« Last Edit: March 22, 2010, 09:02:50 pm by circular »
Conscience is the debugger of the mind

huwmungous

  • New Member
  • *
  • Posts: 14
Re: 'Fashion Model' Graphics.
« Reply #2 on: March 18, 2010, 05:47:48 pm »
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

  • Hero Member
  • *****
  • Posts: 2972
    • Personal webpage
Re: 'Fashion Model' Graphics.
« Reply #3 on: March 18, 2010, 07:31:10 pm »
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;
« Last Edit: March 18, 2010, 07:32:59 pm by circular »
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2972
    • Personal webpage
Re: 'Fashion Model' Graphics.
« Reply #4 on: March 19, 2010, 01:31:27 am »
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.
Conscience is the debugger of the mind

huwmungous

  • New Member
  • *
  • Posts: 14
Re: 'Fashion Model' Graphics.
« Reply #5 on: March 22, 2010, 09:33:25 am »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 2496
Re: 'Fashion Model' Graphics.
« Reply #6 on: March 22, 2010, 10:09:45 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
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

circular

  • Hero Member
  • *****
  • Posts: 2972
    • Personal webpage
Re: 'Fashion Model' Graphics.
« Reply #7 on: March 22, 2010, 09:06:57 pm »
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 !  :)

Conscience is the debugger of the mind

huwmungous

  • New Member
  • *
  • Posts: 14
Re: 'Fashion Model' Graphics.
« Reply #8 on: March 23, 2010, 05:42:10 pm »
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 ...