LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2010, 12:32 PM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Rotate picture?


How do i rotate a picture in C without using any libraries unless they can be compiled on Linux, Windows, Dos protected mode and Dos real mode?
 
Old 03-19-2010, 12:57 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
If you don't have to worry about encoding/decoding common image file formats and will only need to rotate images in 90 degree increments, then it might be feasible to do it without image libraries.
 
Old 03-19-2010, 12:59 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
The image loading code is already handled.
The image is in a two demention array and needs to be rotated in 1 or 22 degree increments from the center-point.
 
Old 03-19-2010, 01:04 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Assuming that (0, 0) is the center of the image, iterate through every point on the image, rotate the point around (0, 0) using trigonometry, and apply the color at the original point on the old image to the new point on the new image.

Last edited by MTK358; 03-19-2010 at 01:05 PM.
 
Old 03-19-2010, 01:09 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I think this might work:

Code:
void rotatePoint(int *x, int *y, double radians) {
	d = sqrt((*x * *x) + (*y * *y));
	t = atan2(*x, *y);
	t += radians;
	*x = sin(t) * d;
	*y = cos(t) * d;
}
I cannot guarantee the correctness of this, because my trigonometry is a little rusty, so it might not work as expected.
 
Old 03-19-2010, 01:21 PM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
I believe the degrees to radian equations is radians = (degrees / 58).
Is that correct?
 
Old 03-19-2010, 01:29 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
360 / (2 * pi) = 57.295...

So I guess 58 is close enough.
 
Old 03-19-2010, 01:41 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Uh-oh:
Code:
atan2: DOMAIN error
Floating point error: Domain.
Abnormal program termination
Code:
#include <math.h>
#include <stdio.h>
void rotatePoint(int *x, int *y, double radians) {
 double d = sqrt((*x * *x) + (*y * *y));
 double t = atan2(*x, *y);
 t += radians;
 *x = sin(t) * d;
 *y = cos(t) * d;
}
int main(){
char array[4][4] = {
"0#00",
"0#00",
"0#00",
"0#00"
};
for(int x = 0;x < 4;x++){
for(int y = 0;y < 4;y++){
int jx = x;
int jy = y;
rotatePoint(&jx, &jy, (double)90/(double)58);
array[jx][jy] = array[x][y];
}
}
for((int) x = 0;x < 4;x++){
for(int y = 0;y < 4;y++){
printf("%c", array[x][y]);
}
}
return 0;
}
 
Old 03-19-2010, 01:42 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Please indent your code.

Most good editors intended for coding retain the current indentation level when you make a new line, so you just press Tab when you start a block and Backspace when you end one. Even with editors that don't do that, IMHO pressing tab after every line doesn't do any harm.

Last edited by MTK358; 03-19-2010 at 01:47 PM.
 
Old 03-19-2010, 01:48 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
The best i can do:
Code:
#include <math.h>
#include <stdio.h>
void rotatePoint(int *x, int *y, double radians) {
	double d = sqrt((*x * *x) + (*y * *y));
	double t = atan2(*x, *y);
	t += radians;
	*x = sin(t) * d;
	*y = cos(t) * d;
}
int main(){
	char array[4][4] = {
		"0#00",
		"0#00",
		"0#00",
		"0#00"
	};
	for(int x = 0;x < 4;x++){
		for(int y = 0;y < 4;y++){
			int jx = x;
			int jy = y;
			rotatePoint(&jx, &jy, (double)90/(double)58);
			array[jx][jy] = array[x][y];
		}
	}
	for((int) x = 0;x < 4;x++){printf("\n");
		for(int y = 0;y < 4;y++){
			printf("%c", array[x][y]);
		}
	}
	return 0;
}
 
Old 03-19-2010, 01:54 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
The indentation is pretty good.

Any way, I don't really understand the error. Maybe try this:

double t = atan2((double) *x, (double) *y);

instead of this:

double t = atan2(*x, *y);
 
Old 03-19-2010, 01:57 PM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
IMPORTANT:

I just thought that if you take each point in the old image, rotate it, and put it in the new one, it's likely that some pixels will me missed in the new image.

It is probably a much better idea to iterate through the points of the new, empty image, rotate the point backwards, and read that point from the original image.
 
Old 03-19-2010, 02:05 PM   #13
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
#include <math.h>
#include <stdio.h>
void rotatePoint(int *x, int *y, double radians) {
	double d = sqrt((*x * *x) + (*y * *y));
	double t = atan2((double)*x, (double)*y);
	t += radians;
	*x = sin(t) * d;
	*y = cos(t) * d;
}
int main(){
	char array[4][4] = {
		"0#00",
		"0#00",
		"0#00",
		"0#00"
	};
	char paw[4][4];
	for(int x = 4;x > 0;x--){
		for(int y = 4;y > 0;y--){
			int jx = x;
			int jy = y;
			rotatePoint(&jx, &jy, -(double)90/(double)58);
			paw[jx][jy] = array[x][y];
		}
	}
	for((int) x = 0;x < 4;x++){
		printf("\n");
		for(int y = 0;y < 4;y++){
			printf("%c", paw[x][y]);
		}
	}
	return 0;
Output:
Code:
*###
 ☼Γ
►☻
! ☻%
 
Old 03-19-2010, 02:26 PM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
What is that supposed to mean?
 
Old 03-19-2010, 02:48 PM   #15
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Desired output is
Code:
0000
####
0000
0000
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
lastlog - What is it, and how do I rotate it? ifm Linux - Newbie 9 04-22-2011 09:45 AM
why can't I get picture in picture on skype? radiodee1 Debian 1 05-20-2009 08:10 AM
Rotate display -- Option "Rotate" "CCW" Brad.Scalio@noaa.gov Linux - General 0 01-27-2009 05:32 AM
rotate one screen anton007 Linux - Software 0 06-09-2007 04:25 PM
Log Rotate MaverickApollo Linux - Software 3 01-19-2004 12:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration