Escalando uma imagem usando o vizinho mais próximo
Eu tenho tentado fazer meu programa ampliar uma imagem. Ocorreu um problema ao alocar novo espaço para minha imagem em escala, mas acho que ela foi corrigida. O problema que estou tendo é que o programa falha quando estou tentando enviar minha imagem de volta do meu suporte de memória temporário.
A imagem carregada é colocada no meustruct
Image
. Os pixels são colocados emimg->pixels
, a altura emimg->height
e a largura emimg->width
. Mas não tenho idéia do motivo pelo qual o programa falha quando transfiro os pixels do meutmp2
struct
para o meuimg
struct
enquanto não falha quando eu faço o oposto. Aqui está o código:
void makeBigger(Image *img, int scale) {
Image *tmp2;
tmp2 = (Image*)malloc(sizeof(Image));
tmp2->height = img->height*scale;
tmp2->width = img->width*scale;
tmp2->pixels = (Pixel**)malloc(sizeof(Pixel*)*tmp2->height);
for (unsigned int i = 0; i < img->height; i++)
{
tmp2->pixels[i] = (Pixel*)malloc(sizeof(Pixel)*tmp2->width);
for (unsigned int j = 0; j < img->width; j++)
{
tmp2->pixels[i][j] = img->pixels[i][j];
}
}
free(img->pixels);
//scaling up the struct's height and width
img->height *= scale;
img->width *= scale;
img->pixels = (Pixel**)malloc(sizeof(Pixel*)*img->height);
for (unsigned int i = 0; i < tmp2->height; i++)
{
img->pixels[i] = (Pixel*)malloc(sizeof(Pixel)*img->width);
for (unsigned int j = 0; j < tmp2->width; j++)
{
img->pixels[i][j] = tmp2->pixels[i+i/2][j+j/2];
}
}
}
Ficaria feliz se você tiver alguma idéia de como fazer o método do vizinho mais próximo funcionar.
EDIT: Estou tentando cortar o retângulo interno para que eu possa aumentá-lo (zoom).
Image *tmp = (Image*)malloc(sizeof(Image));
tmp->height = img->height / 2;
tmp->width = img->width / 2;
tmp->pixels = (Pixel**)malloc(sizeof(Pixel*) * tmp->height);
for (unsigned i = img->height / 4 - 1; i < img->height - img->height / 4; i++) {
tmp->pixels[i] = (Pixel*)malloc(sizeof(Pixel) * tmp->width);
for (unsigned j = img->width / 4; j < img->width - img->width / 4; j++) {
tmp->pixels[i][j] = img->pixels[i][j];
}
}
for (unsigned i = 0; i < img->height; i++) {
free(img->pixels[i]);
}
free(img->pixels);
img->height = tmp->height;
img->width = tmp->width;
img->pixels = tmp->pixels;
free(tmp);