I've been stuck in this problem for a couple of days now, and most of the information I find is for other languages and well ... I'm trying to rotate a 90 degree BMP image. I have been stuck in this method.
void rotarI(BMP* img,char* nom){
BMP** new;
int* newrows;
int* newcols;
FILE *archivo; //Puntero de tipo FILE para abrir el archivo de imagen
archivo = fopen(nom,"rb+"); //abre la imagen a partir de la ruta, rb+ quiere decir que se va a leer el archivo
if(!archivo){ //si no se encuentra la imagen en la ruta dada
printf("No se encontro la imagen %s",nom);
exit(0);
}
//Lee la cabecera de la imagen y almacena en la estructura a la que apunta la imagen
fseek(archivo,0, SEEK_SET);
fread(&img->bm,sizeof(char),2, archivo);
fread(&img->tamano,sizeof(int),1, archivo);
fread(&img->reservado,sizeof(int),1, archivo);
fread(&img->offset,sizeof(int),1, archivo);
fread(&img->tamanoMetadatos,sizeof(int),1, archivo);
fread(&img->alto,sizeof(int),1, archivo);
fread(&img->ancho,sizeof(int),1, archivo);
fread(&img->numPlanos,sizeof(short int),1, archivo);
fread(&img->profColor,sizeof(short int),1, archivo);
fread(&img->tipoCompresion,sizeof(int),1, archivo);
fread(&img->tamEstruc,sizeof(int),1, archivo);
fread(&img->pxmh,sizeof(int),1, archivo);
fread(&img->pxmv,sizeof(int),1, archivo);
fread(&img->colorUsa,sizeof(int),1, archivo);
fread(&img->colorImp,sizeof(int),1, archivo);
//Se reserva memoria
*new = (BMP*)malloc(img->alto*img->ancho*sizeof(BMP));
int c,r;//counters for rows and columns
*newrows = img->ancho;
*newcols = img->alto;
BMP *o;
BMP *n;
for(r = 0; r < img->alto; r++)
{
for(c = 0; c < img->ancho; c++ )
{
n = (*new) + (c * img->alto) + (img->alto + r + 1 );
o = ((img) + (r * img->ancho) + c);
*n = *o;
}//end of for
}//end of for
fclose(archivo);
}
I'm not very good at c so by searching the internet I found some snippets of code which I'm using as a guide
The code fragment that I am trying to modify is this:
int rotate(PIXEL* original, int rows, int cols, int rotation,
PIXEL** new, int* newrows, int* newcols)
{
int c,r;//counters for rows and columns
*newrows = cols;
*newcols = rows;
//allocating space for the new rotated image
*new = (PIXEL*)malloc(rows*cols*sizeof(PIXEL));
PIXEL *o;
PIXEL *n;
for(r = 0; r < rows; r++)
{
for(c = 0; c < cols; c++ )
{
n = (*new) + (c * rows) + (rows + r + 1 );
o = ((original) + (r * cols) + c);
*n = *o;
}//end of for
}//end of for
return 0;
}
I would like them to shake my hand and guide me.