hello, im doing a assigment which requires me to extract a rectangle from a 30 by 30 picture with pixel 0-65535 into a new file at the same position. this is wat i have gotton so far:
do
{
printf("=========Menu=========\n");
printf("6. Cut and Paste\n");
printf("Choose an option: ");
scanf("%d", &option);
switch(option)
{
case 6:
cut(array1);
break;
}
}while(n==1);
}
void cut(unsigned short array1[SIZE])
{
int cox1,coy1,cox2,coy2,x,y,i;
do{
printf("\nEnter the first X co-ordinate(0-30): ");
scanf("%d", &cox1);
}while(cox1=<30 && cox1=>0);
do{
printf("\nEnter the first Y co-ordinate(0-30): ");
scanf("%d", &coy1);
}while(coy1=<30 && coy1=>0);
do{
printf("\nEnter the second X co-ordinate(0-30): ");
scanf("%d", &cox2);
}while(cox2=<30 && cox2=>0);
do{
printf("\nEnter the second Y co-ordinate(0-30): ");
scanf("%d", &coy2);
}while(coy2=<30 && coy2=>0);
To find the position of an x,y pixel, just do this:
offset=bitmap+x*advance+y*pitch
bitmap is a pointer to the first pixel. advance is the size of a single pixel. Since bitmap will probably be a pointer to an unsigned short (or some other 16-bit integer type), advance should be 1. pitch is the size of a single row. Some image formats have padding at the end of each row to align them to 32 or 64 bits, but this is probably no the case, so pitch will just be 30.
By the way, if the width and height of the image is 30, then the only valid coordinates are [0;29], not [0;30]. Be careful with that or you may cause a segmentation fault.
i need to know how to sieve thru all the values and only get the values in the rectangle.
Well, yes. And to do that, you first need to find where the rectangle the user entered begins, and for that, you use that formula I gave you.
And I just noticed something else. If you're entering the coordinates of the opposite corners of the rectangle, then you should know that to get the width or height of the rectangle, the formula is w=x2-x1+1 and h=y2-y1+1.
Example: if x1=0 and x2=10 then width should be 11 (pixels 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10), which is 10-0+1, or x2-x1+1.