We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Looking for a way to rotate an image...

Dippen
Dippen asked
on
Medium Priority
420 Views
Last Modified: 2010-04-03
Hi!

How can I do the following: Load an TImage from disk (BMP) or from an TJPEGImage, rotate the image 90, 180 or 270 degrees and then save the image to disk again? I can figure out the loading and saving just fine, but how do I rotate the picture? The picture will be about 800x600 pixels big.

Thanks!

//Daniel, Sweden
Comment
Watch Question

Author

Commented:
Edited text of question

Commented:
This questions has been answered previously - look through the archives.

Commented:
But in case this was all you wanted to know:

POINTS CAN BE ROTATED THROUGH AN ANGLE theta ABOUT THE ORIGIN. A ROTATION IS DEFINED MATHEMATICALLY BY

x' = x * costheta - y * sintheta,
y' = x * sintheta + y * costheta.

Commented:
Question 306 dealt with this.

Commented:
This is not a so easy but something like this,
You will need 2 TImage objects (may be one temporary) one original, and one new,
with Canvas.Pixels, properiti of TImage you can read&write pixels with TColor, now the only thing you need to do is to preform procedure that gets a pixels from original and put them into new image, thas oll folcs

Commented:
Question 306 dealt with this.

Commented:
I DONT UNDERSTAND HOW ODESSAS PROPOSED ANSWER ANSWERS THE QUESTION - HE COMPLETELY MISSED THE POINT WHICH WAS HOW TO ROTATE THE IMAGE.  WHATS GOING ON?

Commented:
I POSTED THIS EARLIER, BUT IT DIDNT GET THROUGH.

POINTS CAN BE ROTATED THROUGH AN ANGLE theta ABOUT THE ORIGIN. A ROTATION IS DEFINED MATHEMATICALLY BY

x' = x * costheta - y * sintheta,
y' = x * sintheta + y * costheta.

Commented:
I POSTED THIS EARLIER, BUT IT DIDNT GET THROUGH.

POINTS CAN BE ROTATED THROUGH AN ANGLE theta ABOUT THE ORIGIN. A ROTATION IS DEFINED MATHEMATICALLY BY

x' = x * costheta - y * sintheta,
y' = x * sintheta + y * costheta.

Commented:
errr.. sorry about all that chaps.  I've had a stressful few days.

Commented:
heapster,
if you only have 90, 180 or 270 degrees rotation, you will not need sin or cos, because for those angles their value is an integer. To speed the rotation procedure it up, you just pre-code the transform values (e.g. for 90 degrees, x' would be x * 0 - y * -1).
About your comments, everybody is free to answer a question, even if the answer is totaly wrong or missed the point. Dippen has to decide this by accepting or rejecting it.
You do not need to re-post the same comment several times. Unlike internet newsgroups, it is ensured that once you posted a comment it stays posted. And please, stop shouting around.

Dippen,
Here's some code that is quite fast. Create a form, drop a button and 2 images on it. Load a bitmap into the first image and set the second image to AutoSize=true. Also, add this to your form declaration :

  private
    { Private declarations }
    RotImage : TBitmap;
    RotAngle : Integer;
  end;

Create handlers for the forms OnCreate and the buttons OnClick event and use this code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  RotImage := TBitmap.Create;
  RotAngle := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var XIndex,
    YIndex,
    TargetWidth,
    TargetHeight,
    XTarget,
    YTarget      : Integer;
    SrcCanvas,
    DstCanvas    : TCanvas;
    SaveCursor   : TCursor;
begin
  SaveCursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  Inc(RotAngle);
  if RotAngle = 4 // 360 degrees
   then RotAngle := 0; // 0 degrees
  if (RotAngle = 1) or (RotAngle = 3) then // 90 or 270 degrees
   begin
     TargetWidth := Image1.Picture.Bitmap.Height;
     TargetHeight := Image1.Picture.Bitmap.Width
   end
  else // 0 or 180 degrees
   begin
     TargetHeight := Image1.Picture.Bitmap.Height;
     TargetWidth := Image1.Picture.Bitmap.Width;
   end;
  RotImage.FreeImage;
  RotImage.Height := TargetHeight;
  RotImage.Width := TargetWidth;
  SrcCanvas := Image1.Picture.Bitmap.Canvas;
  DstCanvas := RotImage.Canvas;
  // step through pixels
  try
  for XIndex := 0 to Image1.Picture.Bitmap.Width - 1 do
   for YIndex := 0 to Image1.Picture.Bitmap.Height - 1 do
    begin
      if RotAngle = 0 then // 0 degrees
       begin
         XTarget := XIndex;
         YTarget := YIndex;
       end;
      if RotAngle = 1 then // 90 degrees
       begin
         XTarget := YIndex;
         YTarget := XIndex;
       end;
      if RotAngle = 2 then // 180 degrees
       begin
         XTarget := TargetWidth - XIndex - 1;
         YTarget := TargetHeight - YIndex - 1;
       end;
      if RotAngle = 3 then // 270 degrees
       begin
         XTarget := TargetWidth - YIndex - 1;
         YTarget := TargetHeight - XIndex - 1;
       end;
      DstCanvas.Pixels[XTarget, YTarget] := SrcCanvas.Pixels[XIndex, YIndex];
    end;
  Image2.Picture.Assign(RotImage);
  finally
    Screen.Cursor := SaveCursor;
  end;
end;

All you have to do is to code the images LoadFromFile and SaveToFile routines. Another thing not mentioned here is to pre-set the destination bitmaps palette (if it is palettized) to the source bitmaps palette.

Have fun,
Slash/d003303

Author

Commented:
Odessa! I haven't got a clue what you're trying to say...

Heapster! I will take a look at question 306...

D003303! I tried your code and it worked, but what you mean by "quite fast" is a riddle to me :-). To rotate an image 800x600 pixels on my P75 took 2.15 minutes!!! That's NOT acceptable!

But even the simplest shareware-program can complete the same task in 2-3 seconds maximum. There must be some component/assembler source available that completes the task in that time!!

Any comments?


Author

Commented:
OK...

Let's all agree that a rotation of an image that big (800x600 pixels) can't be done in Object Pascal. I tried the code in question 306 (309 nowadays) as well and it was to slow too.
Now I'm increasing the points hoping that anyone can guide me to a component or assembler source or whatever that can perform a 90/180/270 degree rotation of an TImage in less than 2 seconds. As I said before, every single image-handling program on the market can accomplish it in that time, so it's not impossible!?

Commented:
One third of the solution (180 degrees) can be achieve with CopyRect :

source_rect.Top:=
source_rect.Bottom:=
source_rect.Left:=
source_rect.Right:=
dest_rect.Top:=source_rect.Top;
dest_rect.Bottom:=source_rect.Bottom;
dest_rect.Left:=source_rect.Left;
dest_rect.Right:=source_rect.Right;
moncanvas.CopyRect(dest_rect, travail.Canvas, source_rect);

Others require assembly, but don't know anything about this :-(

Commented:
Use CopyRect.... the easiest way

Use given formulas ...

Or use some freeware components ...

torry.rimini.com/vcl/graphics/rotatedimage.zip
torry.rimini.com/vcl/graphics/rotpic.zip

Regards Zif.

Commented:
Yo,
quite fast means that my code was optimized to reduce iterated method calls that take time, like assigning a Source and destination canvas as helper variables. I know that it is slow compared to assembler stuff. It should just give an idea on how it works.

Slash/d003303

Commented:
A quick question, should the app run on NT 4 only ? Then it would be quite simple.

Slash/d003303

Author

Commented:
No, W95 or NT...

Commented:
Have you visited the TORI pages lately ...

Well I just saw to components by accident, because I don't work with images that much, but If they don't work I'm almost sure you will find something else you can use there ...
http://torry.rimini.com
I found the two components I mentioned under the "Expert Add-Onns & Property Editors" and under "Expert Add-Onns & Property Editors #3"
1.  Imagevue V 1.0.7
2. Olivier Dhan Glyph Property Editor V 1.1
Both of them is Freeware
Have fun, hope it helps
EM

Commented:
Yo,
the GetPixel and SetPixel API calls are the bottleneck. I'm nearly finished to get it run really FAST with my own routines. Hang on !

Slash/d003303

Author

Commented:
Sorry, Michaue, I couldn't find any useful components for this purpose on the page you suggested. (The two components ZifNab suggested was too slow too.)

d003303: I'm eagerly waiting. If you could solve this I would be a very happy programmer!

//Dippen
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Fabolous work, d003303! Thanks alot for the help!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.