Changeset 0576df9 in mainline for uspace/app/viewer/viewer.c


Ignore:
Timestamp:
2020-11-14T21:28:35Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
63b35c7
Parents:
38f5598
Message:

Decoding images without libdraw

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/viewer/viewer.c

    r38f5598 r0576df9  
    3434 */
    3535
    36 #include <draw/surface.h>
    37 #include <draw/codec.h>
    3836#include <errno.h>
     37#include <gfximage/tga.h>
    3938#include <stdbool.h>
    4039#include <stdio.h>
     
    5857
    5958static ui_window_t *window;
    60 static surface_t *surface = NULL;
     59static gfx_bitmap_t *bitmap = NULL;
    6160static ui_image_t *image = NULL;
    6261static gfx_context_t *window_gc;
    6362
    64 static surface_coord_t img_width;
    65 static surface_coord_t img_height;
    66 
    67 static bool img_load(const char *, surface_t **);
    68 static bool img_setup(gfx_context_t *, surface_t *);
     63static gfx_rect_t img_rect;
     64
     65static bool img_load(gfx_context_t *gc, const char *, gfx_bitmap_t **,
     66    gfx_rect_t *);
     67static bool img_setup(gfx_context_t *, gfx_bitmap_t *, gfx_rect_t *);
    6968
    7069static void wnd_close(ui_window_t *, void *);
     
    115114
    116115        if (update) {
    117                 surface_t *lsface;
    118 
    119                 if (!img_load(imgs[imgs_current], &lsface)) {
     116                gfx_bitmap_t *lbitmap;
     117                gfx_rect_t lrect;
     118
     119                if (!img_load(window_gc, imgs[imgs_current], &lbitmap, &lrect)) {
    120120                        printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
    121121                        exit(4);
    122122                }
    123                 if (!img_setup(window_gc, lsface)) {
     123                if (!img_setup(window_gc, lbitmap, &lrect)) {
    124124                        printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
    125125                        exit(6);
     
    128128}
    129129
    130 static bool img_load(const char *fname, surface_t **p_local_surface)
     130static bool img_load(gfx_context_t *gc, const char *fname,
     131    gfx_bitmap_t **rbitmap, gfx_rect_t *rect)
    131132{
    132133        int fd;
     
    158159        vfs_put(fd);
    159160
    160         *p_local_surface = decode_tga(tga, stat.size, SURFACE_FLAG_SHARED);
    161         if (*p_local_surface == NULL) {
     161        rc = decode_tga(gc, tga, stat.size, rbitmap, rect);
     162        if (rc != EOK) {
    162163                free(tga);
    163164                return false;
     
    166167        free(tga);
    167168
    168         surface_get_resolution(*p_local_surface, &img_width, &img_height);
    169 
     169        img_rect = *rect;
    170170        return true;
    171171}
    172172
    173 static bool img_setup(gfx_context_t *gc, surface_t *local_surface)
    174 {
    175         surface_coord_t w, h;
    176         gfx_bitmap_params_t params;
    177         gfx_bitmap_alloc_t alloc;
    178         gfx_bitmap_t *bmp;
     173static bool img_setup(gfx_context_t *gc, gfx_bitmap_t *bmp, gfx_rect_t *rect)
     174{
    179175        gfx_rect_t arect;
    180176        gfx_rect_t irect;
     
    184180        ui_res = ui_window_get_res(window);
    185181
    186         surface_get_resolution(local_surface, &w, &h);
    187         gfx_bitmap_params_init(&params);
    188         params.rect.p1.x = w;
    189         params.rect.p1.y = h;
    190 
    191182        ui_window_get_app_rect(window, &arect);
    192         gfx_rect_translate(&arect.p0, &params.rect, &irect);
    193 
    194         alloc.pitch = sizeof(uint32_t) * w;
    195         alloc.off0 = 0;
    196         alloc.pixels = surface_direct_access(local_surface);
    197 
    198         rc = gfx_bitmap_create(gc, &params, &alloc, &bmp);
    199         if (rc != EOK) {
    200                 surface_destroy(local_surface);
    201                 return false;
    202         }
     183        gfx_rect_translate(&arect.p0, rect, &irect);
    203184
    204185        if (image != NULL) {
    205                 ui_image_set_bmp(image, bmp, &params.rect);
     186                ui_image_set_bmp(image, bmp, rect);
    206187                (void) ui_image_paint(image);
    207188                ui_image_set_rect(image, &irect);
    208189        } else {
    209                 rc = ui_image_create(ui_res, bmp, &params.rect, &image);
     190                rc = ui_image_create(ui_res, bmp, rect, &image);
    210191                if (rc != EOK) {
    211192                        gfx_bitmap_destroy(bmp);
    212                         surface_destroy(local_surface);
    213193                        return false;
    214194                }
     
    218198        }
    219199
    220         if (surface != NULL)
    221                 surface_destroy(surface);
    222 
    223         surface = local_surface;
     200        if (bitmap != NULL)
     201                gfx_bitmap_destroy(bitmap);
     202
     203        bitmap = bmp;
    224204        return true;
    225205}
     
    235215{
    236216        const char *display_spec = DISPLAY_DEFAULT;
    237         surface_t *lsface;
     217        gfx_bitmap_t *lbitmap;
     218        gfx_rect_t lrect;
    238219        bool fullscreen = false;
    239220        gfx_rect_t rect;
     
    287268        }
    288269
    289         if (!img_load(imgs[imgs_current], &lsface)) {
    290                 printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
    291                 return 1;
    292         }
    293 
    294270        // TODO Fullscreen mode
    295271        if (fullscreen) {
     
    306282        viewer.ui = ui;
    307283
    308         rect.p0.x = 0;
    309         rect.p0.y = 0;
    310         rect.p1.x = img_width;
    311         rect.p1.y = img_height;
    312 
     284        /*
     285         * We don't know the image size yet, so create tiny window and resize
     286         * later.
     287         */
    313288        ui_wnd_params_init(&params);
    314289        params.caption = "Viewer";
     290        params.rect.p0.x = 0;
     291        params.rect.p0.y = 0;
     292        params.rect.p1.x = 1;
     293        params.rect.p1.y = 1;
     294
     295        rc = ui_window_create(ui, &params, &window);
     296        if (rc != EOK) {
     297                printf("Error creating window.\n");
     298                return 1;
     299        }
     300
     301        window_gc = ui_window_get_gc(window);
     302
     303        ui_window_set_cb(window, &window_cb, (void *) &viewer);
     304
     305        if (!img_load(window_gc, imgs[imgs_current], &lbitmap, &lrect)) {
     306                printf("Cannot load image \"%s\".\n", imgs[imgs_current]);
     307                return 1;
     308        }
     309
    315310        /*
    316311         * Compute window rectangle such that application area corresponds
    317312         * to rect
    318313         */
    319         ui_wdecor_rect_from_app(&rect, &wrect);
     314        ui_wdecor_rect_from_app(&lrect, &wrect);
    320315        off = wrect.p0;
    321         gfx_rect_rtranslate(&off, &wrect, &params.rect);
    322 
    323         rc = ui_window_create(ui, &params, &window);
    324         if (rc != EOK) {
    325                 printf("Error creating window.\n");
    326                 return 1;
    327         }
    328 
    329         window_gc = ui_window_get_gc(window);
    330 
    331         ui_window_set_cb(window, &window_cb, (void *) &viewer);
    332 
    333         if (!img_setup(window_gc, lsface)) {
     316        gfx_rect_rtranslate(&off, &wrect, &rect);
     317        rc = ui_window_resize(window, &rect);
     318        if (rc != EOK) {
     319                printf("Error resizing window.\n");
     320                return 1;
     321        }
     322
     323        if (!img_setup(window_gc, lbitmap, &lrect)) {
    334324                printf("Cannot setup image \"%s\".\n", imgs[imgs_current]);
    335325                return 1;
Note: See TracChangeset for help on using the changeset viewer.