rectangular cut and paste

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:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 900

unsigned short array1[SIZE];
void cut(unsigned short array1[SIZE]);

void main()
{
int option,n=1;

FILE *fp;

fp = fopen("E:\\ArrayQ.bin", "rb");
fread(array1, sizeof(unsigned short), SIZE, fp);


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);

x=cox2-cox1;
y=coy2-coy1;
array3=(int*)malloc(sizeof(int)*(x*y));
for(i=0,i<SIZE,i++)
{

}



i have trouble getting the value of the values inside the rectangle. help? tx
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.
sorry im still learning c++, so i dont quite understand wat u are saying.

i do not need to find the position of the pixel.
i need to know how to sieve thru all the values and only get the values in the rectangle.

the program prompts the user to enter 2 sets of co-ordinates to form a rectangle.

tx

EDIT: the image is a .bin file which is 16bit grayscale
Last edited on
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.
Last edited on
ok, i used ur formula to get the number where the rectangle begins.

so how do i sieve thru the values now?

tx
Topic archived. No new replies allowed.