Recent

Author Topic: TImage question  (Read 9145 times)

henrytj

  • Jr. Member
  • **
  • Posts: 51
TImage question
« on: September 01, 2007, 04:27:26 am »
I am manipulating an image in a TImage object using Canvas.Pixels. But when I do a SaveToFile I seem to bet getting the old image and not the new modified one. Is there something between the WorkBitmap.Canvas.Pixels changes and the WorkBitmap.SaveToFile that I need to be doing?

Thanks,
Henry

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
RE: TImage question
« Reply #1 on: September 01, 2007, 12:33:44 pm »
No problem here. The following works as expected for me (GTK):

  Image1.Picture.Bitmap.LoadFromFile('/home/theo/logoop.bmp');
  Image1.Picture.Bitmap.Canvas.Pen.Color:=clred;
  Image1.Picture.Bitmap.Canvas.MoveTo(1,1);
  Image1.Picture.Bitmap.Canvas.LineTo(50,50);
  Image1.Picture.Bitmap.Canvas.Pixels[2,2]:=clwhite;
  Image1.Picture.Bitmap.SaveToFile('/home/theo/logoopn.bmp');

henrytj

  • Jr. Member
  • **
  • Posts: 51
TImage question
« Reply #2 on: September 01, 2007, 03:29:18 pm »
Could there be a difference between the Image form control and the TBitmap object? I am doing roughly what you did in your test but doing it "behind the scenes" with a TBitmap object. Or there is a flaw in my logic that I can't seem to see.

Here is the code in case someone can see what I am doing wrong.

Code: [Select]

Procedure SaveFinalImage (ImageFileName : String);

Var
   x, y : Integer;
   PixelCalc : TThisPixel;

Begin
For y := 0 To ArrayMax_y Do
    Begin
    For x := 0 To ArrayMax_x Do
        Begin
        With WorkBitmap.Canvas, PixelWorkArray[x, y] Do
             Begin
             If ImageCount > 0 Then
                Begin
                //PixelCalc.RedVal   := Round (PixelWorkArray[x, y].RedVal   / ImageCount);
                //PixelCalc.GreenVal := Round (PixelWorkArray[x, y].GreenVal / ImageCount);
                //PixelCalc.BlueVal  := Round (PixelWorkArray[x, y].BlueVal  / ImageCount);
                PixelCalc.RedVal   := Round (RedVal   / ImageCount);
                PixelCalc.GreenVal := Round (GreenVal / ImageCount);
                PixelCalc.BlueVal  := Round (BlueVal  / ImageCount);
                Pixels[x, y] := RGBToColor (PixelCalc.RedVal,
                                            PixelCalc.GreenVal,
                                            PixelCalc.BlueVal);
                //Pixels[x, y] := ClRed;
                End
             Else
                 Begin
                 Pixels[x, y] := clBlack;
                 End;//If-Else
             End;//With
        End;//For
    End;//For
WorkBitmap.SaveToFile(ImageFileName);
End;

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
TImage question
« Reply #3 on: September 01, 2007, 03:57:35 pm »
This is not enough information for me to see the problem.
Avoid "with" statements while debugging. They are error prone and hard to read for others.
What is done with WorkBitmap before calling this? Is it a global variable?
What's ImageCount?
And most important: How does the result look after calling this procedure?

henrytj

  • Jr. Member
  • **
  • Posts: 51
TImage question
« Reply #4 on: September 01, 2007, 05:17:44 pm »
I never used to use With statements before, but due to the very long lines of code I thought it would help.

What I am doing is loading a series of images one at a time into the program and summing up their pixel RGB values  into the PixelWorkArray declared at the Unit level, so I guess it is global is scoping to the Unit. With some test code (not shown) I am satisfied that the pixel RGB values are being summed correctly. The ImageCount is a count of the images that have been summed so that I can calculate the average pixel values across many images. Basically I am averaging many images together to see what special effect I can get out of it. The program is 90%} done. The last part that doesn't seem to be working is going from calculated average going back into the WorkBitmap and it saving to disk. What I am getting as a result of the SaveToFile is the last image that I loaded (LoadFromFile) that I Put into the WorkBitmap (also defined at the Unit level.)

Blending images together like this is difficult to do in PS and why this is my first Pascal project to automate this. I worked with Delphi i little about 10 years ago, but used Pascal extensively back in college about 25 years ago. But that was pre GUI and OOP, of course.

I was hoping to finish this by the weekend as I need to travel. I spent about 6 hours on it yesterday searching the forums and FP PDF docs. At one time I could have slapped something like this together in a couple hours rather than the days it is taking me. But again, I have not programmed in 5+ years.
Sadly I dont have time to work on it anymore today. Frustrating in that it is all but done, save for this last snag.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
TImage question
« Reply #5 on: September 01, 2007, 06:09:11 pm »
I guess it's something in the logic of you code that has not directly to do with the code you showed us above.
Another hint:
When doing a lot of pixel operations, your approach may be slow.
Try using a TLazIntfImage as shown here
http://wiki.lazarus.freepascal.org/Developing_with_Graphics#A_fading_example

Read the part about Lazarus, not Delphi!

henrytj

  • Jr. Member
  • **
  • Posts: 51
TImage question
« Reply #6 on: September 03, 2007, 04:39:20 am »
After a couple more hours of experimenting I found that if I add the following 3 commands

Code: [Select]

WorkBitmap.FreeImage;
WorkBitmap.Width  := 320;
WorkBitmap.Height := 240;


Then things work.

Apparently I have to "free" the image and then set the size values again.

Henry

 

TinyPortal © 2005-2018