void pixMap_rotate (pixMap *p, float theta){ //correct rotation that uses inverse rotation to determine which pixels to copy from the original //the previous method would leave holes in the image pixMap *temp=pixMap_init();//make a new temp blank pixMap structure pixMap_copy(temp,p); //copy the p to temp //set the values in the image to zero using memset - that the 3rd argument of memset is the size in BYTES memset(temp->image,0,temp->width*temp->height*sizeof(rgba)); const float ox=p->width/2.0f; const float oy=p->height/2.0f; const float s=sin(degreesToRadians(-theta)); const float c=cos(degreesToRadians(-theta)); //for() //double for loop to loop through each pixel in the original //for() //calculate the old coordinates rotx roty to copy from using the formula from //http://stackoverflow.com/questions/2259476/rotating-a-point-about-another-point-2d //use the answer from stackoverflowery //round the coordinates to the nearest integer in your calculations (add 0.5 and cast to integer) //if rotated coordinates are still inside the image //copy the pixel at the old coords to the pixel to the temporary copy using memcpy //i.e. memcpy(temp->pixArray[y]+x,p->pixArray[roty]+rotx,sizeof(rgba)) // // // //copy the temp pixMap to the original //destroy the temp for(int y=0;y<p->height;y++){ for(int x=0;x<p->width;x++){ float rotx = c*(x-ox) - s * (oy-y) + ox; float roty = -(s*(x-ox) + c * (oy-y) - oy); int rotj=rotx+.5; int roti=roty+.5; if(roti >=0 && roti < temp->height && rotj >=0 && rotj < temp->width){ memcpy(temp->pixArray[y]+x,p->pixArray[roti]+rotj,sizeof(rgba)); } } } pixMap_copy(p,temp); pixMap_destroy(temp); return; }
int main(int argc, char *argv[]) { char *inputfile = 0, *outputfile = 0; float degrees = 0, grayFlag = 0; int count; for(count = 0; count < argc; count++) { /*Print arguments for reference*/ //fprintf(stderr, "argv[%d] %s \n", count, argv[count]); /*Check for input*/ if(strcmp(argv[count], "-i") == 0) { inputfile = argv[count + 1]; count++; /*Check for output*/ } else if(strcmp(argv[count], "-o") == 0) { outputfile = argv[count + 1]; count++; /*Check for rotate*/ } else if(strcmp(argv[count], "-r") == 0) { degrees = strtof(argv[count + 1], NULL); count++; /*Check for gray*/ } else if(strcmp(argv[count], "-g") == 0) { grayFlag = 1; count++; } } pixMap *p=pixMap_init_filename(inputfile); if(degrees)pixMap_rotate(p,degrees); if(grayFlag)pixMap_gray(p); pixMap_write(p,outputfile); pixMap_destroy(p); return 0; }