Changeset 0576df9 in mainline


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

Location:
uspace
Files:
6 added
10 edited

Legend:

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

    r38f5598 r0576df9  
    3434 */
    3535
    36 #include <draw/surface.h>
    37 #include <draw/codec.h>
    3836#include <device/led_dev.h>
    3937#include <errno.h>
    4038#include <fibril_synch.h>
     39#include <gfximage/tga_gz.h>
    4140#include <io/pixel.h>
    4241#include <loc.h>
     
    9493static fibril_timer_t *frame_timer = NULL;
    9594static ui_image_t *frame_img;
    96 static surface_t *frames[FRAMES];
    9795static gfx_bitmap_t *frame_bmp[FRAMES];
    9896
     
    123121static bool decode_frames(gfx_context_t *gc)
    124122{
    125         gfx_bitmap_alloc_t alloc;
    126         surface_coord_t w, h;
    127         gfx_bitmap_params_t params;
     123        gfx_rect_t rect;
    128124        errno_t rc;
    129125
    130126        for (unsigned int i = 0; i < FRAMES; i++) {
    131                 frames[i] = decode_tga_gz(images[i].addr, images[i].size,
    132                     SURFACE_FLAG_SHARED);
    133                 if (frames[i] == NULL) {
     127                rc = decode_tga_gz(gc, images[i].addr, images[i].size,
     128                    &frame_bmp[i], &rect);
     129                if (rc != EOK) {
    134130                        printf("Unable to decode frame %u.\n", i);
    135131                        return false;
    136132                }
    137133
    138                 surface_get_resolution(frames[i], &w, &h);
    139                 gfx_bitmap_params_init(&params);
    140                 params.rect.p1.x = w;
    141                 params.rect.p1.y = h;
    142 
    143                 alloc.pitch = sizeof(uint32_t) * w;
    144                 alloc.off0 = 0;
    145                 alloc.pixels = surface_direct_access(frames[i]);
    146 
    147                 rc = gfx_bitmap_create(gc, &params, &alloc, &frame_bmp[i]);
    148                 if (rc != EOK) {
    149                         printf("Error creating bitmap.\n");
    150                         return false;
    151                 }
     134                (void) rect;
    152135        }
    153136
     
    162145                gfx_bitmap_destroy(frame_bmp[i]);
    163146                frame_bmp[i] = NULL;
    164 
    165                 surface_destroy(frames[i]);
    166                 frames[i] = NULL;
    167147        }
    168148}
  • uspace/app/barber/meson.build

    r38f5598 r0576df9  
    2727#
    2828
    29 deps = [ 'ui', 'draw', 'compress' ]
     29deps = [ 'ui', 'gfximage', 'compress' ]
    3030
    3131_images = files(
  • uspace/app/launcher/launcher.c

    r38f5598 r0576df9  
    3636#include <errno.h>
    3737#include <gfx/coord.h>
     38#include <gfximage/tga.h>
    3839#include <stdbool.h>
    3940#include <stdio.h>
     
    4243#include <str_error.h>
    4344#include <task.h>
    44 
    45 #include <draw/surface.h>
    46 #include <draw/source.h>
    47 #include <draw/drawctx.h>
    48 #include <draw/codec.h>
    49 
    5045#include <ui/fixed.h>
    5146#include <ui/image.h>
     
    157152        gfx_bitmap_params_t logo_params;
    158153        gfx_bitmap_t *logo_bmp;
    159         gfx_bitmap_alloc_t alloc;
    160154        gfx_context_t *gc;
    161         surface_coord_t w, h;
    162155        gfx_rect_t logo_rect;
    163156        gfx_rect_t rect;
     157        gfx_coord2_t off;
    164158        errno_t rc;
    165159
     
    182176        }
    183177
    184         surface_t *logo = decode_tga((void *) helenos_tga, helenos_tga_size,
    185             SURFACE_FLAG_SHARED);
    186         if (!logo) {
    187                 printf("Unable to decode logo.\n");
    188                 return 1;
    189         }
    190 
    191178        rc = ui_create(display_spec, &ui);
    192179        if (rc != EOK) {
     
    217204        gc = ui_window_get_gc(window);
    218205
    219         surface_get_resolution(logo, &w, &h);
     206        rc = decode_tga(gc, (void *) helenos_tga, helenos_tga_size,
     207            &logo_bmp, &logo_rect);
     208        if (rc != EOK) {
     209                printf("Unable to decode logo.\n");
     210                return 1;
     211        }
     212
    220213        gfx_bitmap_params_init(&logo_params);
    221         logo_params.rect.p1.x = w;
    222         logo_params.rect.p1.y = h;
    223         logo_rect = logo_params.rect;
    224 
    225         alloc.pitch = sizeof(uint32_t) * w;
    226         alloc.off0 = 0;
    227         alloc.pixels = surface_direct_access(logo);
    228 
    229         rc = gfx_bitmap_create(gc, &logo_params, &alloc, &logo_bmp);
    230         if (rc  != EOK) {
    231                 printf("Error creating bitmap.\n");
    232                 return 1;
    233         }
     214        logo_params.rect = logo_rect;
    234215
    235216        rc = ui_fixed_create(&launcher.fixed);
     
    245226        }
    246227
    247         rect.p0.x = 5;
    248         rect.p0.y = 32;
    249         rect.p1.x = 5 + w;
    250         rect.p1.y = 32 + h;
     228        off.x = 5;
     229        off.y = 32;
     230        gfx_rect_translate(&off, &logo_rect, &rect);
    251231        ui_image_set_rect(launcher.image, &rect);
    252232
  • uspace/app/launcher/meson.build

    r38f5598 r0576df9  
    2727#
    2828
    29 deps = [ 'draw', 'ui' ]
     29deps = [ 'gfximage', 'ui' ]
    3030
    3131_images = files('gfx/helenos.tga')
  • uspace/app/viewer/meson.build

    r38f5598 r0576df9  
    2727#
    2828
    29 deps = [ 'ui', 'draw', 'compress' ]
     29deps = [ 'ui', 'gfximage', 'compress' ]
    3030src = files('viewer.c')
    3131
  • 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;
  • uspace/lib/meson.build

    r38f5598 r0576df9  
    8383        'ext4',
    8484        'gfxfont',
     85        'gfximage',
    8586        'hound',
    8687        'ipcgfx',
  • uspace/lib/ui/include/ui/window.h

    r38f5598 r0576df9  
    5353extern void ui_window_add(ui_window_t *, ui_control_t *);
    5454extern void ui_window_remove(ui_window_t *, ui_control_t *);
     55extern errno_t ui_window_resize(ui_window_t *, gfx_rect_t *);
    5556extern ui_resource_t *ui_window_get_res(ui_window_t *);
    5657extern gfx_context_t *ui_window_get_gc(ui_window_t *);
  • uspace/lib/ui/src/window.c

    r38f5598 r0576df9  
    215215}
    216216
     217/** Resize/move window.
     218 *
     219 * Resize window to the dimensions of @a rect. If @a rect.p0 is not 0,0,
     220 * the top-left corner of the window will move on the screen accordingly.
     221 *
     222 * @param window Window
     223 * @param rect Rectangle
     224 *
     225 * @return EOK on success or an error code
     226 */
     227errno_t ui_window_resize(ui_window_t *window, gfx_rect_t *rect)
     228{
     229        gfx_coord2_t offs;
     230        gfx_rect_t nrect;
     231        errno_t rc;
     232
     233        /*
     234         * Move rect so that p0=0,0 - keep window's coordinate system origin
     235         * locked to top-left corner of the window.
     236         */
     237        offs = rect->p0;
     238        gfx_rect_rtranslate(&offs, rect, &nrect);
     239
     240        /* dwindow can be NULL in case of unit tests */
     241        if (window->dwindow != NULL) {
     242                rc = display_window_resize(window->dwindow, &offs, &nrect);
     243                if (rc != EOK)
     244                        return rc;
     245        }
     246
     247        ui_wdecor_set_rect(window->wdecor, &nrect);
     248        ui_wdecor_paint(window->wdecor);
     249        return EOK;
     250}
     251
    217252/** Set window callbacks.
    218253 *
  • uspace/lib/ui/test/window.c

    r38f5598 r0576df9  
    175175}
    176176
     177/** ui_window_resize */
     178PCUT_TEST(resize)
     179{
     180        errno_t rc;
     181        ui_t *ui = NULL;
     182        ui_wnd_params_t params;
     183        ui_window_t *window = NULL;
     184        gfx_rect_t nrect;
     185
     186        rc = ui_create_disp(NULL, &ui);
     187        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     188
     189        ui_wnd_params_init(&params);
     190        params.caption = "Hello";
     191        params.rect.p0.x = 0;
     192        params.rect.p0.y = 0;
     193        params.rect.p1.x = 1;
     194        params.rect.p1.y = 1;
     195
     196        rc = ui_window_create(ui, &params, &window);
     197        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     198        PCUT_ASSERT_NOT_NULL(window);
     199
     200        nrect.p0.x = -1;
     201        nrect.p0.y = -1;
     202        nrect.p1.x = 2;
     203        nrect.p1.y = 2;
     204        rc = ui_window_resize(window, &nrect);
     205        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     206
     207        ui_window_destroy(window);
     208        ui_destroy(ui);
     209}
     210
    177211/** ui_window_get_res/gc/rect() return valid objects */
    178212PCUT_TEST(get_res_gc_rect)
Note: See TracChangeset for help on using the changeset viewer.