ftp.nice.ch/pub/next/graphics/viewer/ToyViewer.2.6.s.tar.gz#/ToyViewer2.6/src/ImageOpr.bproj/rotate.c

This is rotate.c in view mode; [Download] [Up]

#include  <libc.h>
#include  <math.h>
#include  "../common.h"
#include  "../getpixel.h"


static double cosine, sine;
static int xdiff, ydiff;

void rotate_size(int angle, commonInfo *cinf, commonInfo *newinf)
{
	if (angle == 90 || angle == 270) {
		newinf->width = cinf->height;
		newinf->height = cinf->width;
	}else if (angle == 0 || angle == 180) {
		newinf->width = cinf->width;
		newinf->height = cinf->height;
	}else {
		double th = ((double)angle * 3.14159265) / 180.0;
		double co = cos(th);
		double si = sin(th);
		if (co < 0) co = -co;
		if (si < 0) si = -si;
		newinf->width = cinf->width * co + cinf->height * si + 0.5;
		newinf->height = cinf->width * si + cinf->height * co + 0.5;
		if (angle > 270) {
			xdiff = cinf->height * si - 0.5;
			ydiff = 0.0;
		}else if (angle > 180) {
			xdiff = newinf->width - 1;
			ydiff = cinf->height * co - 0.5;
		}else if (angle > 90) {
			xdiff = cinf->width * co - 0.5;
			ydiff = newinf->height - 1;
		}else {
			xdiff = 0.0;
			ydiff = cinf->width * si - 0.5;
		}
		cosine = cos(th);
		sine = sin(th);
	}
}


int sub_rotate(int op, int angle, commonInfo *cinf, commonInfo *newinf,
			int idx[], unsigned char **working)
{
	int	x, y;
	int	i, pidx, ptr;
	int	pix[MAXPLANE];

	if (op == Horizontal) {
		for (y = 0; y < newinf->height; y++) {
			ptr = y * newinf->width;
			for (x = newinf->width - 1; x >= 0; x--) {
				getPixelA(pix);
				for (i = 0; i <= ALPHA; i++) {
					if ((pidx = idx[i]) < 0) continue;
					working[pidx][ptr + x] = pix[i];
				}
			}
		}
	}else if (op == Vertical) {
		for (y = newinf->height - 1; y >= 0; y--) {
			ptr = y * newinf->width;
			for (x = 0; x < newinf->width; x++) {
				getPixelA(pix);
				for (i = 0; i <= ALPHA; i++) {
					if ((pidx = idx[i]) < 0) continue;
					working[pidx][ptr + x] = pix[i];
				}
			}
		}
	}else if (angle == 90) {
		for (x = 0; x < newinf->width; x++) {
			for (y = newinf->height - 1; y >= 0; y--) {
				ptr = y * newinf->width;
				getPixelA(pix);
				for (i = 0; i <= ALPHA; i++) {
					if ((pidx = idx[i]) < 0) continue;
					working[pidx][ptr + x] = pix[i];
				}
			}
		}
	}else if (angle == 270) {
		for (x = newinf->width - 1; x >= 0; x--) {
			for (y = 0; y < newinf->height; y++) {
				ptr = y * newinf->width;
				getPixelA(pix);
				for (i = 0; i <= ALPHA; i++) {
					if ((pidx = idx[i]) < 0) continue;
					working[pidx][ptr + x] = pix[i];
				}
			}
		}
	}else if (angle == 180) {
		for (y = newinf->height - 1; y >= 0; y--) {
			ptr = y * newinf->width;
			for (x = newinf->width - 1; x >= 0; x--) {
				getPixelA(pix);
				for (i = 0; i <= ALPHA; i++) {
					if ((pidx = idx[i]) < 0) continue;
					working[pidx][ptr + x] = pix[i];
				}
			}
		}
	}else /* any */ {
		unsigned char *tmp[MAXPLANE];
		int dx, dy, ox, oy, pty;
		int pl = cinf->numcolors;
		if (newinf->alpha) pl++;
		if (allocImage(tmp, cinf->width, cinf->height, 8, pl))
			return Err_MEMORY;
		for (y = 0; y < cinf->height; y++) {
			ptr = y * cinf->width;
			for (x = 0; x < cinf->width; x++) {
				getPixelA(pix);
				for (i = 0; i <= ALPHA; i++) {
					if ((pidx = idx[i]) < 0) continue;
					tmp[pidx][ptr + x] = pix[i];
				}
			}
		}
		for (y = 0; y < newinf->height; y++) {
			dy = y - ydiff;
			ptr = y * newinf->width;
			for (x = 0; x < newinf->width; x++) {
				dx = x - xdiff;
				ox = dx * cosine - dy * sine + 0.5;
				oy = dx * sine + dy * cosine + 0.5;
				if (ox < 0 || ox >= cinf->width
				 || oy < 0 || oy >= cinf->height) {
				    for (i = 0; i < pl; i++)
					working[i][ptr + x] = 0;
				}else {
				    pty = oy * cinf->width + ox;
				    for (i = 0; i < pl; i++)
					working[i][ptr + x] = tmp[i][pty];
				}
			}
		}
		free((void *)tmp[0]);
	}
	return 0;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.