Link to home
Start Free TrialLog in
Avatar of Robert Bowden
Robert Bowden

asked on

I'll give the points to get this answered.

My friend and I, (He has asked this question here but no one will answer him) are devloping an App.  We want to change an image once scanned or loaded from a file.
Example: Our Timage now contains a .BMP we want to be able to contrast, lighten, darken, change red value, and blue level.  The last two red and blue are not as critical as contrast, lighten and darken.  Will somebody please post the code for this.  We don't want to buy a book.  All we want is the code. Thanks
Rob.

As I am giving my full points, I would expect a little more in the way of an answer.  We are both new to Delphi and maybe a (grin, grin) small routine would help out?  I know "bad joke".  How about it? Anyone have a routine?
I did the code myself, but it did not work.  I translated what you had.  Delphi did it, but not the whole image, just one pixel.  Oh well, I'll have to wait or drop the idea.
I used Canvas.Pixels I could only get one pixel to change.  I think it requires more than what Delphi offers.  I have afriend in Denmark and he said it would require a lot of code.  Who knows, I would like to do it, but I guess I won't be. Okay, how do you get the RGB out of TCOLOR?  This code to lighten etc... will be valuable to a lot people.  I wrote a freeware Twain component and I get tons of requests for it. See what I mean? the intense stuff is hot.  I don't sell components.  I write software for myself. It relaxes me. (Ya right) Andrew does the same.  It's a curiosity thing.

Partial solution.
We have discovered that if you place a scrollbar on the screen and in the onchange handler go
Mainform.Color := RGB(Scrollbar1.Position,ScrollBar1.Postion,ScrollBar.Position) that the form will change.  Now the problem is how to do this with an image?  I think maybe something to do with the canvas and pixels.  But how to determine how many pixels?  Any suggestions?
Maybe I should explain.  I have only been using Delphi for about a month.  I sort get what you mean.  I am getting frustrated.  I don't believe that there is an answer to this.  Is there a way or not a way?  Would someone please tell me the code? I figured out the first part now all i need is to know:
How to do it in memory.
How to do the whole image at once.
Thanks
I figured it out anyway.  It's not done with Pixels[x,y] it's a little more than that.  
Avatar of gysbert1
gysbert1

Contrast is a bit more tricky, but lighten and darken can be done
by simply iterating through all the pixels in the TImage using the pixels property, and adding the same value to the Red, Green, and blue components of the pixel value. You can obviously also adjust the red and blue levels separately. I'm not sure offhand how to do contrast...
Here's some very simple pseudo-code.

For X := 0 to width - 1
    For Y := 0 to height - 1
      Read Pixel RGB
      If (you want to lighten image)
         increase each RGB component by an equal amount
      If (you want to darken image)
         decrease each RGB component by an equal amount)
      If (you want to work on red)
         increase/decrease R by some amount
      If (you want to work on blue)
         increase/decrease B by some amount
      If (you want to work on green)
         increase/decrease G by some amount

This is very easy to implement.  Look at the TColor type in
help for info on altering the RGB value.  Look at the Pixel
property of canvases....canvas.pixel[x, y] = A...

This is how you'd do it true colour mode...if you are working with palettes, then there are quicker, but more complex ways of doing this using palette animation...but I'm not about to go into that...I now refuse to work with palettes...they are a thing of the past in my opinion...4MB video cards are too damn inexpensive to be working with palettes.

Also, make sure you don't do this on the TImage itself.
Do all these calculations on a bitmap buffer (in memory), then
copy the bitmap to the TImage after completion.  It'll be
much faster  this way.
ASKER CERTIFIED SOLUTION
Avatar of mheacock
mheacock

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Bowden

ASKER

Edited text of question
I don't have the time...so you can reject...someone should be able to convert the psuedo code to Delphi code with no problems..an easy 200+ points (if you grade well).
Contrast is trickier...I might be able to get an algorithm for you.  BTW, you might want to try converting the pseudo-code yourself...you can learn a lot by actually trying to do the coding yourself...(not a bad joke).
As an 'unpaid' expert I view myself as more of a college lab instructor than a paid consultant who will do the code for you...as I see it, my job here is to show you the road not taken help you along, help you learn, get you going in the right directions, not spoon feed you or anyone.
Edited text of question
Avatar of StevenB
In that I answered the first question for 15 points with pretty much the same advice given above perhaps someone could have the decency to either grade, reject or respond to my answer rather than just asking it again somewhere else?
If you don't know how to get the RGB components out of TColor,
ask and you shall receive some code on how to do that.

As for the pseudo-code above, anyone with any programming experience in any language (well, perhasps not Prolog or Lisp)
can convert it.
Post your code and I'll see if I can help you refine what
you've got and get it working...I'm willing to help you work
on code you already have.
Edited text of question
Edited text of question
Edited text of question
Each pixel on a canvas can be referenced by X,Y coordinates using the Pixel[X,Y:LongInt] property. This property yields a Colour. This property can also be used to set a colour. So then:

Simply loop through all the pixels on the canvas (two nested loops 0-width & 0-height) and change the colours. It will not require a lot of code and should be finnished in time for someone to grade my answer to the first instance of this question asked elsewhere.
Edited text of question
Reject my answer...I no longer have time to answer questions
here...it is much too time consuming.
Edited text of question
So?  What did you figure out?  Clue us all in.
 Yes I'm facinated too, this has become almost a soap opera.
Here for example is some code to brighten the image (Note that doing it this way is very easy but incredibly slow)

var
  x, y : integer;
  l : longint;
  t : integer;
  blue, green, red : integer;
begin
  for y:=0 to Image1.Picture.Height-1 do
            for x:=0 to Image1.Picture.Width-1 do
      begin
                        l:=Image1.Canvas.Pixels[x,y];
        blue:=(l shr 16) and $ff;
        green:=(l shr 8) and $ff;
        red:=l and $ff;
        blue:=blue+20;
        red:=red+20;
        green:=green+20;
        if red>$ff then red:=$ff;
        if blue>$ff then blue:=$ff;
        if green>$ff then green:=$ff;
        l:=(blue shl 16) + (green shl 8) + red;
        Image1.Canvas.Pixels[x,y]:=l;
      end;
end;