SDK Multiplataforma en C logo

SDK Multiplataforma en C

Imágenes desde URLs

❮ Anterior
Siguiente ❯

En esta demo construimos un sencillo visor de imágenes Web. El programa permite descargarlas y visualizarlas mediante una lista desplegable. El código fuente está en la caperta /src/howto/urlimg de la distribución del SDK.

Captura de un visor de imágenes en versión Windows.
Figura 1: Versión Windows
Captura de un visor de imágenes en versión macOS.
Figura 2: Versión macOS
Captura de un visor de imágenes en versión Linux.
Figura 3: Versión Linux
Listado 1: howto/urlimg/urlimg.c
  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
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Images from URL */

#include "nappgui.h"
#include "inet.h"
#include "httpreq.h"

typedef struct _app_t App;

struct _app_t
{
    Window *window;
    ImageView *view;
    uint32_t selected;
    Label *imgname;
    Label *imgsize;
    Label *imgres;
    Label *imgformat;
};

static const char_t *i_HOST = "test.nappgui.com";
static const char_t *i_URI = "/image_formats";
static const char_t *i_FILES[] = {
                            "anim_04_bat.gif",
                            "anim_04_cube.gif",
                            "anim_04_dragon.gif",
                            "anim_04_game.gif",
                            "anim_04_item.gif",
                            "anim_04_player.gif",
                            "home_01_gray.jpg",
                            "home_01_rgb.jpg",
                            "home_02_index02.png",
                            "home_02_index02c.png",
                            "home_02_index04.png",
                            "home_02_index04c.png",
                            "home_02_index16.png",
                            "home_02_index16c.png",
                            "home_02_index256.png",
                            "home_02_index256c.png",
                            "home_02_rgba.png",
                            "home_02_rgbac.png",
                            "sea_01_gray.jpg",
                            "sea_01_rgb.jpg",
                            "sea_02_index02.png",
                            "sea_02_index02c.png",
                            "sea_02_index04.png",
                            "sea_02_index04c.png",
                            "sea_02_index16.png",
                            "sea_02_index16c.png",
                            "sea_02_index256.png",
                            "sea_02_index256c.png",
                            "sea_02_rgb.png",
                            "sea_02_rgbc.png",
                            "sea_03_bmp_index02.bmp",
                            "sea_03_bmp_index04.bmp",
                            "sea_03_bmp_index16.bmp",
                            "sea_03_bmp_index256.bmp",
                            "sea_03_bmp_rgb.bmp" };

/*---------------------------------------------------------------------------*/

static __INLINE String *i_pixformat(const pixformat_t format, const uint32_t ncolors)
{
    switch (format) {
    case ekINDEX1:
        return str_printf("Indexed 1bbp (%d colors)", ncolors);
    case ekINDEX2:
        return str_printf("Indexed 2bbp (%d colors)", ncolors);
    case ekINDEX4:
        return str_printf("Indexed 4bbp (%d colors)", ncolors);
    case ekINDEX8:
        return str_printf("Indexed 8bbp (%d colors)", ncolors);
    case ekGRAY8:
        return str_c("Gray8");
    case ekRGB24:
        return str_c("RGB24");
    case ekRGBA32:
        return str_c("RGBA32");
    case ekFIMAGE:
    case ekOPTIMAL:
    cassert_default();
    }
    return str_c("");
}

/*---------------------------------------------------------------------------*/

static void i_download(App *app)
{
    String *uri = str_printf("%s/%s", i_URI, i_FILES[app->selected]);
    ierror_t err;
    Http *http = http_create(i_HOST, 80, &err);
    if (http != NULL)
    {
        uint32_t status = 0;
        http_get(http, tc(uri), NULL, 0, &err);
        status = http_response_status(http);
        if (status >= 200 && status <= 299)
        {
            Stream *stm = stm_memory(8096);

            if (http_response_body(http, stm, &err) == TRUE)
            {
                uint32_t width, height;
                pixformat_t format;
                uint32_t ncolors = 0;
                uint64_t start = stm_bytes_readed(stm);
                Image *image = image_read(stm);
                uint64_t end = stm_bytes_readed(stm);
                String *ssize = str_printf("%d bytes", (uint32_t)(end - start));
                String *sres = NULL;
                String *sformat = NULL;

                // Test for image pixels read
                if (image_get_codec(image) != ekGIF)
                {
                    Pixbuf *pixdata = NULL;
                    Palette *palette = NULL;
                    image_pixels(image, ekOPTIMAL, &pixdata, &palette);
                    image_destroy(&image);
                    width = pixbuf_width(pixdata);
                    height = pixbuf_height(pixdata);
                    format = pixbuf_format(pixdata);
                    image = image_from_pixbuf(pixdata, palette);
                    pixbuf_destroy(&pixdata);

                    if (palette != NULL)
                    {
                        ncolors = palette_size(palette);
                        palette_destroy(&palette);
                    }
                }
                else
                {
                    image_size(image, &width, &height);
                    image_format(image, &format);
                }

                imageview_image(app->view, image);
                sres = str_printf("%d x %d", width, height);
                sformat = i_pixformat(format, ncolors);
                label_text(app->imgname, i_FILES[app->selected]);
                label_text(app->imgsize, tc(ssize));
                label_text(app->imgres, tc(sres));
                label_text(app->imgformat, tc(sformat));
                image_destroy(&image);
                stm_close(&stm);
                str_destroy(&ssize);
                str_destroy(&sres);
                str_destroy(&sformat);
            }
        }

        http_destroy(&http);
    }

    str_destroy(&uri);
}

/*---------------------------------------------------------------------------*/

static Layout* i_label(const char_t *title, Label **info)
{
    Layout *layout = layout_create(2, 1);
    Label *label = label_create();
    Font *font = font_system(font_regular_size(), ekFBOLD);
    *info = label_create();
    label_text(label, title);
    label_font(label, font);
    layout_label(layout, label, 0, 0);
    layout_label(layout, *info, 1, 0);
    layout_halign(layout, 1, 0, ekJUSTIFY);
    layout_hmargin(layout, 0, 5);
    layout_hexpand(layout, 1);
    font_destroy(&font);
    return layout;
}

/*---------------------------------------------------------------------------*/

static void i_add_files(PopUp *popup)
{
    register uint32_t i, n = sizeof(i_FILES) / sizeof(char_t*);
    for (i = 0; i < n; ++i)
        popup_add_elem(popup, i_FILES[i], NULL);

    popup_list_height(popup, 20);
}

/*---------------------------------------------------------------------------*/

static void i_OnSelect(App *app, Event *e)
{
    const EvButton *p = event_params(e, EvButton);
    app->selected = p->index;
    i_download(app);
}

/*---------------------------------------------------------------------------*/

static Panel *i_panel(App *app)
{
    Panel *panel = panel_create();
    Layout *layout1 = layout_create(2, 1);
    Layout *layout2 = layout_create(1, 2);
    Layout *layout3 = layout_create(1, 5);
    Label *label = label_create();
    PopUp *popup = popup_create();
    ImageView *view = imageview_create();
    app->view = view;
    label_text(label, "Images");
    i_add_files(popup);
    popup_OnSelect(popup, listener(app, i_OnSelect, App));
    imageview_size(view, s2df(600, 400));
    layout_label(layout2, label, 0, 0);
    layout_popup(layout2, popup, 0, 1);
    layout_imageview(layout3, view, 0, 0);
    layout_layout(layout3, i_label("Image name:", &app->imgname), 0, 1);
    layout_layout(layout3, i_label("Image size:", &app->imgsize), 0, 2);
    layout_layout(layout3, i_label("Image dimensions:", &app->imgres), 0, 3);
    layout_layout(layout3, i_label("Image format:", &app->imgformat), 0, 4);
    layout_layout(layout1, layout2, 0, 0);
    layout_layout(layout1, layout3, 1, 0);
    layout_margin(layout1, 5);
    layout_hmargin(layout1, 0, 5);
    layout_vmargin(layout2, 0, 5);
    layout_vmargin(layout3, 0, 5);
    layout_vmargin(layout3, 1, 3);
    layout_vmargin(layout3, 2, 3);
    layout_valign(layout1, 0, 0, ekTOP);
    panel_layout(panel, layout1);
    return panel;
}

/*---------------------------------------------------------------------------*/

static void i_OnClose(App *app, Event *e)
{
    osapp_finish();
    unref(app);
    unref(e);
}

/*---------------------------------------------------------------------------*/

static App *i_create(void)
{
    App *app = heap_new0(App);
    Panel *panel = i_panel(app);
    String *title = str_printf("Images from URL '%s%s'", i_HOST, i_URI);
    app->window = window_create(ekWNSTD, &panel);
    app->selected = 0;
    inet_init();
    i_download(app);
    window_title(app->window, tc(title));
    window_origin(app->window, v2df(500, 200));
    window_OnClose(app->window, listener(app, i_OnClose, App));
    window_show(app->window);
    str_destroy(&title);
    return app;
}

/*---------------------------------------------------------------------------*/

static void i_destroy(App **app)
{
    window_destroy(&(*app)->window);
    inet_finish();
    heap_delete(app, App);
}

/*---------------------------------------------------------------------------*/

#include "osmain.h"
osmain(i_create, i_destroy, "", App)
❮ Anterior
Siguiente ❯