Обрезать изображение файла BMP в программе c - PullRequest
0 голосов
/ 03 июня 2019

Моя функция - получать изображение, и я пытаюсь обрезать изображение BMP по размеру: 100 x 100, 200 x 200, 300 x 300, 400 x 400 по пикселям, я не знаю, что сделать, чтобы это работало,Пожалуйста, помогите мне

Размер изображения - int высота и int ширина, и функция знает значения в пикселях.

Вот мой код:

void re_allocate_pixels(struct RGB_Image *image, int new_height, int new_width)
{
    int org_height = image->height;
    int org_width = image->width;
    int org_size = image->size;
    int i;
    struct Pixel **pxls;
    pxls = (struct Pixel **)malloc((org_height) * sizeof(struct Pixel *));
    if (pxls == NULL)
    {
        printf("Memory allocation failed\n");
        exit(1);
    }
    for (i = 0; i < org_height; i++)
    {
        pxls[i] = (struct Pixel *)malloc(org_width * sizeof(struct Pixel));
        if (pxls[i] == NULL)
        {
            printf("Memory allocation failed\n");
            exit(1);
        }
    }
     //i have no idea what to do next to crop the image and pixecl 
    /*for (int j = 0; j < org_height; j++)
    {
        for (int k = 0; k < org_width; k++)
        {
            pxls[i][j] = pxls[k][j];
        }
    }*/
}

здесьэто данные структуры:

    struct Pixel
    {
        unsigned char red;
        unsigned char green;
        unsigned char blue;
    };
    struct RGB_Image
    {
        char file_name[MAX_FILE_NAME_SIZE];
        long height;
        long width;
        long size;
        struct Pixel **pixels;
    };

вот как я вызываю эту функцию

struct RGB_Image *rgb_img_ptr;
struct RGB_Image image;
rgb_img_ptr = &image;
int image_load_ret_value = load_image(rgb_img_ptr);
re_allocate_pixels(rgb_img_ptr, 100,100); // here is calling 

1 Ответ

1 голос
/ 05 июня 2019

Вы должны удалить старое распределение, а также назначить новое значение size. size обычно относится к "ширине в байтах", умноженной на высоту.

В этом случае значение «ширина в байтах» должно быть «ширина * 3», и оно всегда должно быть кратным 4.

Читайте изображение по одной строке за раз. Используйте mempcy для копирования каждой строки.

Обратите внимание, что растровые изображения обычно перевернуты. Возможно, вам придется изменить цикл на for (int y = dst.height - 1; y >=0; y--){memcpy...}

void re_allocate_pixels(struct RGB_Image *image, int new_height, int new_width)
{
    if(new_width > image->width) return;
    if(new_height > image->height) return;

    struct RGB_Image *src = image;
    struct RGB_Image dst;

    dst.width = new_width;
    dst.height = new_height;

    //allocate memory
    dst.pixels = malloc(dst.height * sizeof(struct Pixel*));
    for(int y = 0; y < dst.height; y++)
        dst.pixels[y] = malloc(dst.width * sizeof(struct Pixel));

    //copy from source to destination
    for(int y = 0; y < dst.height; y++)
        memcpy(dst.pixels[y], src->pixels[y], dst.width * sizeof(struct Pixel));

    //free the old allocation
    for(int y = 0; y < src->height; y++)
        free(image->pixels[y]);
    free(image->pixels);

    //assing new allocation
    image->pixels = dst.pixels;

    //set the new width, height, size
    image->width = dst.width;
    image->height = dst.height;

    int bitcount = 24;
    int bytecount = 3; //<- same as sizeof(struct Pixel)

    //calculate width in bytes
    int width_in_bytes = ((dst.width * bitcount + 31) / 32) * 4;

    image->size = width_in_bytes * dst.height;
}
...