在 C 中缩小图像

问题描述 投票:0回答:1

各位程序员大家好。我是论坛新手,请多多包涵。

我想要实现的是从文件中获取加载的图像(例如背景壁纸图像 4000x2250)并将其缩小到 1360x768 的原始分辨率。当您为桌面设置壁纸图像时,就像现代操作系统一样。

typedef struct {
    UINT8 Blue;
    UINT8 Green;
    UINT8 Red;
    UINT8 Reserved;
} EFI_GRAPHICS_OUTPUT_BLT_PIXEL;

typedef EFI_GRAPHICS_OUTPUT_BLT_PIXEL   GUI_PIXEL;

typedef struct {
    UINT32    Width;
    UINT32    Height;
    BOOLEAN   HasAlpha;
    GUI_PIXEL *PixelData;
} GUI_IMAGE;
GUI_IMAGE* ReduceImage(GUI_IMAGE* image)
{
    UINT32 resizeWidth = image->Width / 2;
    UINT32 resizeHeight = image->Height / 2;
    UINT32 x;
    UINT32 y;
    UINT32 row = 0;
    UINT32 column = 0;
    GUI_IMAGE *NewImage;

    NewImage = AllocateZeroPool(sizeof(GUI_IMAGE));
    if (NewImage == NULL)
    {
        return NULL;
    }

    NewImage->PixelData = AllocateZeroPool(resizeWidth * resizeHeight *
sizeof(GUI_PIXEL));
    if (NewImage->PixelData == NULL) 
    {
        FreePool(NewImage);
        return NULL;
    }

    NewImage->Width = resizeWidth;
    NewImage->Height = resizeHeight;
    NewImage->HasAlpha = TRUE;

    for(y = 0; y < resizeHeight - 1; y++)
    {
        for(x = 0; x < resizeWidth - 1; x++)
        {
            NewImage->PixelData[(resizeWidth) * y + x] = image->PixelData[(image->Width) * row + column];
            column += 2;
        }
        row += 2;
        column = 0;
    }

    return NewImage;
}
c image reduce scaling
1个回答
0
投票

感谢大家的帮助!我明白了。

#define RoundValue(x)  ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

GUI_IMAGE* ScaleImage(GUI_IMAGE* image)
{
    float ratio; // >1 is enlargering(e.g. 1.1), <1 is reducing(e.g. 0.4)
    UINT32 resWidth;
    UINT32 resHeight;
    UINT32 x;
    UINT32 y;
    UINT32 x_in;
    UINT32 y_in;
    GUI_IMAGE *NewImage;

    GetResolution(&resWidth, &resHeight);

    ratio = (float)resWidth / (float)image->Width;

    NewImage = AllocateZeroPool(sizeof(GUI_IMAGE));
    if (NewImage == NULL)
    {
        return NULL;
    }

    NewImage->PixelData = AllocateZeroPool(1360 * 768 * sizeof(GUI_PIXEL));
    if (NewImage->PixelData == NULL) 
    {
        FreePool(NewImage);
        return NULL;
    }

    NewImage->Width = resWidth;
    NewImage->Height = resHeight;
    NewImage->HasAlpha = TRUE;

    for(y = 0; y < resHeight; y++)
    {
        for(x = 0; x < resWidth; x++)
        {
            x_in = RoundValue((float)x / ratio);
            y_in = RoundValue((float)y / ratio);
            NewImage->PixelData[resWidth * y + x] = image->PixelData[image->Width * y_in + x_in];
        }
    }

    return NewImage;
}
© www.soinside.com 2019 - 2024. All rights reserved.