我一直试图让我的程序扩大图像。我在为缩放图像分配新空间时遇到了一些问题,但我认为它是固定的。我遇到的问题是当我试图从我的临时内存持有者发回我的图像时程序崩溃。
加载的图片放在我的struct
Image
中。放置像素
img->pixels
,img->height
中的高度和img->width
中的宽度。但是当我将像素从tmp2
struct
传输到我的img
struct
时,我不知道为什么程序会崩溃,而当我执行相反操作时它不会崩溃。这是代码:
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];
}
}
}
如果您对如何使最近邻方法起作用,我会很高兴。
编辑:我正在尝试裁剪内部矩形,以便我可以将其缩放(缩放)。
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);
答案 0 :(得分:3)
我看到你过于复杂的事情(例如两次走过图像)。
这是代码(我发布了整个程序 - 我对 Pixel 和<做出了假设em>图像可能与您拥有的不匹配),但如果您复制/粘贴 makeBigger ,它应该可以在您的代码中使用 OOTB :
code.c :
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint32_t Pixel;
typedef struct {
uint32_t width, height;
Pixel **pixels;
} Image;
void makeBigger(Image *img, int scale) {
uint32_t i = 0, j = 0;
Image *tmp = (Image*)malloc(sizeof(Image));
tmp->height = img->height * scale;
tmp->width = img->width * scale;
tmp->pixels = (Pixel**)malloc(sizeof(Pixel*) * tmp->height);
for (i = 0; i < tmp->height; i++) {
tmp->pixels[i] = (Pixel*)malloc(sizeof(Pixel) * tmp->width);
for (j = 0; j < tmp->width; j++) {
tmp->pixels[i][j] = img->pixels[i / scale][j / scale];
}
}
for (i = 0; i < img->height; i++)
free(img->pixels[i]);
free(img->pixels);
img->width = tmp->width;
img->height = tmp->height;
img->pixels = tmp->pixels;
free(tmp);
}
void printImage(Image *img) {
printf("Width: %d, Height: %d\n", img->width, img->height);
for (uint32_t i = 0; i < img->height; i++) {
for (uint32_t j = 0; j < img->width; j++)
printf("%3d", img->pixels[i][j]);
printf("\n");
}
printf("\n");
}
int main() {
uint32_t i = 0, j = 0, k = 1;
Image img;
// Allocate and initialize the image data
img.height = 2;
img.width = 3;
img.pixels = (Pixel**)malloc(sizeof(Pixel*) * img.height);
for (i = 0; i < img.height; i++) {
img.pixels[i] = (Pixel*)malloc(sizeof(Pixel) * img.width);
for (j = 0; j < img.width; j++)
img.pixels[i][j] = k++;
}
printImage(&img);
makeBigger(&img, 2);
printImage(&img);
// Deallocate the image data
for (i = 0; i < img.height; i++)
free(img.pixels[i]);
free(img.pixels);
return 0;
}
备注( makeBigger 相关 - 旨在替换作为参数提供的图片内容):
tmp->pixels[i][j] = img->pixels[i / scale][j / scale]
free(img->pixels);
单独会产生内存泄漏)<强>输出强>:
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q041861274]> ls code.c [cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q041861274]> gcc -o code.exe code.c [cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q041861274]> ./code.exe Width: 3, Height: 2 1 2 3 4 5 6 Width: 6, Height: 4 1 1 2 2 3 3 1 1 2 2 3 3 4 4 5 5 6 6 4 4 5 5 6 6