Translation Kit: loading images

Translation Kit: loading images

  • mdo  DigitalBox
  •   KitLab
  •   November 10, 2025

In previous KitLab articles, we focused on the Interface Kit: windows, views, events, layouts, and menus.

Now, let’s move into another useful kit : Translation!

Haiku’s Translation Kit is a powerful system that lets you load and save data in many formats (images, text, …) through “translators”.

For this article, we’ll start with the most common task: loading and displaying an image.


Usage of BTranslationUtils

The Translation Kit provides a utility class, BTranslationUtils, that can create a BBitmap from a file, regardless of its format.

  • PNG, JPEG, BMP, GIF, TIFF, and even SVG are supported — as long as you have the corresponding translator installed.

  • You don’t need to parse the file yourself.

Just call:

BBitmap* bmp = BTranslationUtils::GetBitmapFile("/path/to/image.png");

You get a ready-to-use BBitmap that can be drawn inside any BView.


Example: ImageViewer.cpp

Here’s a minimal image viewer using the Translation Kit:

#include <Application.h>
#include <Window.h>
#include <View.h>
#include <Bitmap.h>
#include <TranslationUtils.h>
#include <stdio.h>

class ImageView : public BView {
public:
    ImageView(BRect frame, BBitmap* bitmap)
        : BView(frame, "image_view", B_FOLLOW_ALL, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
          fBitmap(bitmap)
    {
        SetViewColor(B_TRANSPARENT_COLOR);
    }

    void Draw(BRect rect) override {
        SetDrawingMode(B_OP_ALPHA);
        SetHighColor(255, 255, 255);
        FillRect(Bounds());

        if (fBitmap) {
            BRect imgBounds = fBitmap->Bounds();
            float imgWidth = imgBounds.Width() + 1;
            float imgHeight = imgBounds.Height() + 1;

            // Calculate offset to center the image
            float dx = (Bounds().Width() - imgWidth) / 2.0f;
            float dy = (Bounds().Height() - imgHeight) / 2.0f;

            DrawBitmap(fBitmap, BPoint(dx, dy));
        } else {
            SetHighColor(255, 0, 0);
            DrawString("Failed to load image", BPoint(10, 20));
        }

        SetHighColor(0, 0, 0);
        StrokeRect(Bounds());
    }

private:
    BBitmap* fBitmap;
};

class ImageWindow : public BWindow {
public:
    ImageWindow(const char* filename)
        : BWindow(BRect(100, 100, 500, 400), "Simple Image Viewer",
                  B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE)
    {
        BBitmap* bmp = BTranslationUtils::GetBitmapFile(filename);
        if (!bmp) {
            printf("Could not load %s\n", filename);
            return;
        }

        // Get the image size
        BRect imgBounds = bmp->Bounds();
        float width  = imgBounds.Width() + 1;   // +1 because Width() is inclusive
        float height = imgBounds.Height() + 1;

        // Ensure at least 200x200
        if (width < 200)  width = 200;
        if (height < 200) height = 200;

        // Resize the window to fit the image
        ResizeTo(width, height);

        // Add the view sized exactly to the image
        AddChild(new ImageView(bmp));

        Show();
    }
};

class ImageApp : public BApplication {
public:
    ImageApp()
        : BApplication("application/x-vnd.demo-imageviewer") {}

    void ReadyToRun() override {
        const char* filename = "/boot/system/data/icons/haiku/preferences/32/help-about.svg";
        new ImageWindow(filename);
    }
};

int main() {
    ImageApp app;
    app.Run();
    return 0;
}

How It Works

  1. Load image:
    BTranslationUtils::GetBitmapFile() reads the file and returns a BBitmap.

  2. Window size adapts:
    The window resizes to the image’s size (minimum 200×200).

  3. Drawing:
    The BView::Draw() method centers the image and draws it on a white background.

Ensuring proper redraw on window resize

By default, a BView does not always repaint its entire area when the window is resized, which can lead to visual artifacts (unfinished background, misaligned image, etc.).

To force a complete redraw whenever the window size changes, add the flag B_FULL_UPDATE_ON_RESIZE in the BView constructor:

ImageView::ImageView(BRect frame, BBitmap* bitmap)
    : BView(frame, "image_view",
            B_FOLLOW_ALL, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
      fBitmap(bitmap)
{
    SetViewColor(B_TRANSPARENT_COLOR);
}

This ensures the Draw() method is called again for the entire view whenever the window is resized.


Compile and Run

g++ ImageViewer.cpp -o ImageViewer -lbe -ltranslation

Launch the application:

./ImageViewer

The viewer displays a centered SVG image from the system in the window !

What about replacing the image by another SVG image in the source code and recompile the program ?

Below is the display of the Haiku SVG logo with the same program:

You will notice this is only a few lines of code to have a basic - but powerful - image viewer !


Why It Matters

  • Supports multiple formats without special code.

  • Handles transparency in PNG and SVG automatically.

  • A great starting point for building your own image viewer, editor, or thumbnail browser.


Try this:

Now that the image is correctly displayed and the view redraws properly when resized, here are a few ideas you could experiment with to push the example further — without changing too much code:

  • Let the user choose the image to display
    For example, read the file path with ArgvReceived() or via a file panel

  • Add drag-and-drop support
    Allow users to drag an image file onto the window and have it reload automatically

  • Resize or scale the image
    Adjust the bitmap when the window resizes, instead of keeping the original size centered


What’s Next?

In upcoming KitLab articles, we’ll see how to:

  • Export images to different formats.

  • Work with text translators.

  • Writing a simple translator.

These topics will show how the Translation Kit goes beyond simple image loading and opens the door to handling multiple media types in Haiku applications.

Stay tuned — and keep on exploring!


Powered by Bludit - Hosted by Planet Hoster
© 2025 Haiku Insider