Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

New Developer
Posts: 6
Registered: ‎03-26-2012
My Device: PLaybook 16GB
My Carrier: Rogers

Reading image from file and displaying it

Hi All,


Can someone provide few lines of code showing the usage of img_t structure to read from file, using img_load_file, and display it on device. The critical part would be assignig the pointer to direct.data.




New Contributor
Posts: 6
Registered: ‎03-12-2012
My Device: Non
My Carrier: Non

Re: Reading image from file and displaying it

[ Edited ]

Hello there,


It has been alot of pain for me to do that (as in I had trouble with loading images until I figured it out). Everything is available in the Gesture sample app. Here is the code below:


//-----------------------Load Image---------------------------

static int decode_setup(uintptr_t data, img_t *img, unsigned flags)
    screen_window_t bg_win = (screen_window_t)data;
    screen_buffer_t screen_buf;
    int Fsize[2];

    Fsize[0] = img->w;
    Fsize[1] = img->h;
    screen_set_window_property_iv(bg_win, SCREEN_PROPERTY_BUFFER_SIZE, Fsize);
    screen_create_window_buffers(bg_win, 1);

    screen_get_window_property_pv(bg_win, SCREEN_PROPERTY_RENDER_BUFFERS, (void **)&bg_buf);
    screen_get_buffer_property_pv(bg_buf, SCREEN_PROPERTY_POINTER, (void **)&img->access.direct.data);
    screen_get_buffer_property_iv(bg_buf, SCREEN_PROPERTY_STRIDE, (int *)&img->access.direct.stride);

    img->flags |= IMG_DIRECT;
    return IMG_ERR_OK;

static void decode_abort(uintptr_t data, img_t *img)
    screen_window_t bg_win = (screen_window_t)data;

load_image(screen_window_t bg_win, const char *path)
    img_decode_callouts_t callouts;
    img_lib_t ilib = NULL;
    img_t img;
    int rc;

    rc = img_lib_attach(&ilib);
    if (rc != IMG_ERR_OK) {
        return -1;

    img.flags = 0;

    memset(&img, 0, sizeof(img));
    img.flags |= IMG_FORMAT;
    img.format = IMG_FMT_PKLE_ARGB8888;

    memset(&callouts, 0, sizeof(callouts));
    callouts.setup_f = decode_setup;
    callouts.abort_f = decode_abort;
    callouts.data = (uintptr_t)bg_win;

    rc = img_load_resize_file(ilib, path, &callouts, &img);

    return rc == IMG_ERR_OK ? 0 : -1;

//-----------------------Load Image---------------------------


Just change bg_win and bg_buf with your variables. To load, just use the load_image function with you window name and the path. The path is of the following format: 


const char* path = "app/native/_____.jpg"; /* Relative path to image asset */


Make sure the image is an asset.


Good luck Smiley Happy