Scaling an image

Hi, So i have a background image that I want to scale.
My bitmap image is 640 X 240. And I would like to scale by a factor of 2 to get a result of of 1280 X 480. Where in the code below should I do a multiplication of 2 such that I'm able to scale the image by a factor of 2? I can't understand the code below well enough to know where I should add my scaling factor. Hope someone with more windows GDI programming experience is able to help. If the scaling is not done here, then can I have a rough idea of how to scale? New to windows GDI. Thank you.

Note: The reason I don't post my whole code is because the file is too big.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void RenderedBackground::render() const
{
    const unsigned int width = this->width();
    const unsigned int height = this->height();
    const int rowData = BYTES_PER_PIXEL * width;
    const float dx = 1.0f / static_cast<float>(width);
    const float dy = 1.0f / static_cast<float>(height);
    for (unsigned int h = 0; h < height; ++h)
    {
        for (unsigned int w = 0; w < width; ++w)
        {
            const float wf = static_cast<float>(w);
            const float hf = static_cast<float>(h);
            const Object::PointType ul(wf * dx, (hf + 1.0f) * dy);
            const Object::PointType lr((wf + 1.0f) * dx, hf * dy);
            const POINT pul = getFactory().toDevice(ul);
            const POINT plr = getFactory().toDevice(lr);
            const RECT rect = {pul.x, pul.y, plr.x, plr.y};
            const unsigned int index = h * rowData + BYTES_PER_PIXEL * w;
            const char* data = pixelData();
            HBRUSH brush = CreateSolidBrush(RGB(
                data[index + 2],
                data[index + 1],
                data[index + 0]
            ));
            FillRect(getFactory().getContext(), &rect, brush);
            DeleteObject(brush);
        }
    }
}
Last edited on
Do you want to do your own pixel bashing, or do you want to scale the image?
https://docs.microsoft.com/en-us/windows/win32/gdi/bitmap-scaling
Mmm lets take this example, how do I scale it up instead of down? I know its not c++ but I just need the concept of scaling up. Hope you or anyone is able to help.

1
2
3
4
5
6
7
8
9
10
11
12
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
        boolean filter) {
    float ratio = Math.min(
            (float) maxImageSize / realImage.getWidth(),
            (float) maxImageSize / realImage.getHeight());
    int width = Math.round((float) ratio * realImage.getWidth());
    int height = Math.round((float) ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
            height, filter);
    return newBitmap;
}
Last edited on
Topic archived. No new replies allowed.