Question

Need an efficient C/C++ code for rotating an image

Asked by: ygal02

I need an efficient C or C++ code for rotating an image (in my case it is a float array and two integers representing the dimentions, but anything semilar is ok) by X degrees around the center, using BiCubic interpolation.
Any tips are welcome.
Please note that I'm developing under Linux (no DirectX or OpenGL).
Source code with SSE support will be even better.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-03-09 at 06:37:56ID21343664
Tags

image

,

code

,

rotate

Topic

Graphics & Game Programming

Participating Experts
10
Points
500
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. rotating in 3d
    ok i have a list of points call point1 point2 and so on, i want to rotate them about a vertical and horozontial axis one at a time for exampl the letter A is on the screen made up of a list of points and linked with lines. when the user presses the up or down arrow i want to ...
  2. How to rotate a camera in opengl using delphi
    Dear all, I have written a starter application using opengl and the following tools Gl, Glu, UrickGL, U3Dpolys. I have walls appearing on the screen and can zoom in and move around the map with my mouse but would like to be able to move the camera not the image. When I try to...
  3. Problems with axis rotations!
    Hi! I have a problem with my rotations in my project (using Visual Studio 6 & Directx 8). I have loaded a .x model and with my code I can rotate my object around x,y and z axis (I rotate the object 90 degrees / rotation). Now to my problem.. If I rotate the object 90 d...
  4. DirectX or OpenGL
    I am preparing for my Senior project at my university, and my team is planning to do a 2D RPG/strategy game with sprites and eventually some sound. We will be using C++ as programming language for this project. As far as we see it, we have two ways that we can take this p...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ozoPosted on 2005-03-10 at 09:19:42ID: 13508354

You can do it with 3 simple shear transforms
see Subject 3.01: How do I rotate a bitmap?
in http://www.faqs.org/faqs/graphics/algorithms-faq/

 

by: ozoPosted on 2005-03-10 at 09:28:56ID: 13508463

 

by: ygal02Posted on 2005-03-13 at 00:51:26ID: 13527766

ozo,
Thanks for your comment, but what I'm looking for, is the following:
Rotating an image using Bilinear interpolation is quick and easy, but, unfortunately does not yeild a high quality results. Using Shear transformations on the other hand will be too slow.
It seems to me that Bicubic interpolation is a good compramise. Unfortunately I could not find any efficient algorithms for BiCubic interpolation (it seems to me that since this is a well known problem probably there are well known efficient solutions as well).

Any help is very welcome.

Thanks

 

by: DanMacDonaldPosted on 2005-03-22 at 17:18:45ID: 13607601

Look up the Anti Grain Gemoetry projects, AGG. It's an opensource 2d Graphics project that has code for subpixel accuracy in rotations etc.

 

by: gtokasPosted on 2005-03-28 at 06:02:46ID: 13643439

DirectX have a function to rotate a bitmap....
I tried it myself but didn't make it a year ago...
Maybe now it is possible...
Also GDI+ have such function also...
There are plenty of examples to rotate a bitmap but with a size larger than 200x200 its VERY slow.. About 2secs to do it with P4 2200 and GForceMX440 with 512Mb RAM...
GDI+ functions are 100% faster.. I had 1sec to rotate 5 degrees..
As you can see (the way I made it) both functions were slow...
I don't know about DirectX yet...

gtokas.

 

by: guitaristxPosted on 2005-04-12 at 11:13:42ID: 13765414

Actually, you can do it in two shear operations.

The wisest, as far as view accuracy is concerned, is to make the post-rotated image bigger (in terms of pixel count), so that you can maintain a bit more detail.  Then, use pixel-averaging to dither the image back down:

1. Double the size (or whatever seems sufficient).
2. Shear horizontal
3. Shear vertical
4. Reduce resolution by pixel-averaging

Believe it or not, shearing twice is going to be blazingly fast compared to almost any other method.

 

by: ygal02Posted on 2005-04-12 at 23:26:52ID: 13769753

guitaristx:
Thanks for your comment.
It seems you suggested a linear interpolation for resizing, then rotating and then another linear interpolation for shrinking (?).
In his case the interpolation method probably wont' do the job. I tried a few interpolation methods, including using a resizing operatin prior to the rotation and it seems that the best quality to efficiency ratio is for BiCubic interpolation.
Linear interpolation is not good enough (losing quality too fast).
Using resizing operations on the other hand are not better than BiCubic interpolation but are slower !

I can get some commercial licraries (like Intel's IPP) which has a "Image Rotation" functions, but since my images have some "special" properties I need to write the code myself.

Thanks,
Yaniv

 

by: guitaristxPosted on 2005-04-13 at 06:38:15ID: 13772024

First, I'm not recommending a linear interpolation for resizing, I'm recommending a resize operation that will scale the image up by a whole-number factor -- no interpolation required.

Your problem is that you'll have a trade-off on speed vs. accuracy.  Two shears is how most of the imaging libraries work.  I mentioned resizing the image because you say that you need extra accuracy.  HOWEVER, that extra accuracy will cost you some speed.

I've dealt with this sort of thing before - I used to work as a software engineer for a company that specialized in image processing software.  You just need to make sure that you're avoiding the slow operations: conditions, multiplication, division.

If two shear operations is not accurate enough, then you need to figure out a way to preserve more detail in the image, which means resizing.  The first resize operation (if it's done right) should be relatively cost-free, since all you're doing is doubling the size.  The second resize operation is what will be costly, especially if you're dealing with a color image.  However, there are some tricks:

When you're averaging pixels, you're averaging the values of four bytes, which means that, mathematically, you add the four values together and divide by four:
(p1 + p2 + p3 + p4) / 4 = a
You'll do this once for a grayscale image, and three times for an RGB image.

However, you can change the above formula to look like this:
p1/4 + p2/4 + p3/4 + p4/4 = a

So, now you can generate a 256 byte lookup table where each value is the index divided by four, and you've eliminated division altogether, which makes for a significant speed increase.

What's "special" about your images?

 

by: guitaristxPosted on 2005-04-13 at 06:40:45ID: 13772050

Also, SSE doesn't apply here - you'll be doing integer math exclusively.  MMX would apply.  MMX applies to integer math, whereas SSE applies to floating-point math.

 

by: guitaristxPosted on 2005-04-13 at 06:50:48ID: 13772155

An MMX programming primer:
http://codeproject.com/cpp/mmxintro.asp

 

by: guitaristxPosted on 2005-04-13 at 06:56:22ID: 13772216

> Also, SSE doesn't apply here - you'll be doing integer math exclusively.
> MMX would apply.  MMX applies to integer math, whereas SSE applies to floating-point math.

I just re-read your original post - SSE would apply, since you're dealing with floating-point arrays.

However, are you certain that you need your 'image' data as floating-point?  Can you go into any details about the application/usage for these images?

 

by: ygal02Posted on 2005-04-13 at 23:01:34ID: 13779075

guitarx,
My images are multi-layer images. You can think of it as a 3D image that should be rotated around one of the axes. They must be floating point, as they take part of an accurate optimization algorithm of image processing. Actually I had to make them floating point in order to gain efficiency since originally they were double, so I lost some accuracy.
I liked your tip about the efficient devision using lookup table, I'll keep it in mind.
It seems that although I'm an Image Processing algorithm developer I don't know enough about efficient code writting :(

Please let me know if you can think about anything else.

Thanks.

 

by: guitaristxPosted on 2005-04-14 at 06:58:39ID: 13781620

Using floating-point values for your pixel data is very out-of-the-ordinary for image processing as i know it.  If I may, I would recommend running some analysis on your images, and find out the following:

minimum value
maximum value
minimum exponent
maximum exponent
percentage of data points that have another data point within some threshold (say, .1%)

The range of a single-precision float is (+/-)3.4e(+/-)38. Examine your data, and see what range your pixels fall into.  Use some statistical analysis; ignore the upper and lower 5 or 10%, and consider the minimum and maximum values.

This will tell you whether or not you need a floating-point array to store your pixel data.  If your data tends to fall within a very small range of what float can store, then you can safely reduce the precision of your pixels without sacrificing accuracy.  Using floating-point values is going to slow your algorithm considerably, and I'm trying to warn you against using it unless it's ABSOLUTELY NECESSARY.  It's very possible you could reduce the pixels to an unsigned short (16 bits) and maintain sufficient accuracy.  It also makes that lookup table possible - the table size would be 64k, whereas the lookup table for float would be 4GB!

Float math is slooooow, avoid it if at all possible.

 

by: Paladin_VBPosted on 2005-04-16 at 05:37:39ID: 13797119

const float piover180 = 0.0174532925f;

float x , xinc , y , yinc;

x -= xinc;
y += yinc;

xinc=(float)sin(rotation*piover180) * 0.5;
yinc=(float)cos(rotation*piover180) * 0.5;

 

by: ygal02Posted on 2005-04-16 at 23:44:12ID: 13800154

Paladin_VB,
Can you explain the purpose ?

 

by: makc_the_greatPosted on 2005-05-13 at 10:00:32ID: 13997684

>Two shears is how most of the imaging libraries work.

oh yeah? what about >= 90 degrees? there's more logic involved.

>Can you explain the purpose

well if you'd walk through your image with those steps (if Paladin got em right), you'd have a choppy picture like if viewpoint would be rotated. if you'd plot your image using these to increment output coords, you'd have even choppiest rotated image. that was nice trick back on old, really slow machines, which allowed for only two extra multiplications for whole image to be rotated.

 

by: guitaristxPosted on 2005-05-13 at 12:11:40ID: 13998788

> oh yeah? what about >= 90 degrees? there's more logic involved.

Yes, but the additional logic to handle angles >= 90 degrees is trivial compared to the shear operations.  Shear is still going to be the best thing that I've seen thus far.

 

by: wkokkePosted on 2005-05-18 at 08:55:51ID: 14028265

> oh yeah? what about >= 90 degrees? there's more logic involved.

rotating by 90 degrees exactly is very trivial, so the problem can always be reduced to a rotation  < 90 degrees

 

by: gkatzPosted on 2005-05-18 at 09:53:44ID: 14029035

ygal,

I am not an expert in image processing but I wanted to include a couple of quick notes.

  guitaristx, you talked about using MMX ops.  As far as I understand MMX is actually slows down your program because it does not have it's own register set so it is necessary to clear the registers, reload the registers with your data, perform the ops and then reset the registers, which ends up taking longer than just doing the operations normally.  SSE avoids this by having their own set of registers.  It would be interesting to know if they've fixed the issue with MMX in newer CPUs.  

  Another option for speeding up your process is to perform the biCubic interpolation on the graphics card.  Current graphics cards support 32 bit floating point numbers.  We recently performed some tests and found nice speed improvements when writing a CG program to perform simple image processing algorithms.  One thing to consider is that your users will need to have a compatable graphics card.  A quick tip if you decide to go down this path, you stated that your image is multilayer.  Do not attempt to use a 3d texture to hold your data.  It will be much slower than using a 2d texture and then mapping it to a 3d texture inside of your cg program.

gkatz

 

by: NeveringPosted on 2005-06-09 at 11:57:24ID: 14182734

gkatz, is it possible to use the GPU without DirectX or OpenGL, as he said he was using niether.

 

by: ygal02Posted on 2005-07-04 at 05:14:27ID: 14361951

Venabili,

Neither of the experts actually answered my question.
I would like to give to give 100 points to guitaristx and 20 points to each one of the other participates for their good will.

Thanks.

 

by: VenabiliPosted on 2005-07-04 at 05:16:04ID: 14361962

Post a request here:http://www.experts-exchange.com/Community_Support/

Also - please in the future, try to answer to the experts:)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...