]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/examples/libs/libpng/main.c
Inital import
[l4.git] / l4 / pkg / examples / libs / libpng / main.c
1 #include <l4/re/c/namespace.h>
2 #include <l4/re/c/dataspace.h>
3 #include <l4/re/c/rm.h>
4 #include <l4/re/c/util/cap_alloc.h>
5 #include <l4/re/c/util/video/goos_fb.h>
6 #include <l4/libpng/l4png_wrap.h>
7 #include <l4/util/util.h>
8
9 #include <stdio.h>
10
11 int main(int argc, char **argv)
12 {
13   l4_addr_t bildmem;
14   void *vidmem;
15   l4re_util_video_goos_fb_t gfb;
16   l4re_video_view_info_t fbi;
17
18   if (argc < 2)
19     {
20       printf("Need to give PNG picture to display\n");
21       return 1;
22     }
23
24   if (l4re_util_video_goos_fb_setup_name(&gfb, "fb"))
25     return 45;
26
27   if (!(vidmem = l4re_util_video_goos_fb_attach_buffer(&gfb)))
28     return 46;
29
30   printf("size: %ld\n", l4re_ds_size(l4re_util_video_goos_fb_buffer(&gfb)));
31   printf("Vidmem attached to %p\n", vidmem);
32
33   if (l4re_util_video_goos_fb_view_info(&gfb, &fbi))
34     {
35       printf("l4re_fb_open failed\n");
36       return 1;
37     }
38
39   l4re_ds_t bild = l4re_util_cap_alloc();
40   if (l4_is_invalid_cap(bild))
41     return 1;
42   if (l4re_ns_query_srv(l4re_get_env_cap("rom"), argv[1], bild))
43     return -1;
44
45   printf("Picture size: %ld\n", l4re_ds_size(bild));
46
47   bildmem = 0;
48   if (l4re_rm_attach((void **)&bildmem, l4re_ds_size(bild),
49                      L4RE_RM_SEARCH_ADDR, bild, 0, L4_PAGESHIFT))
50     return 1;
51
52
53   int png_w, png_h;
54   png_get_size_mem((void *)bildmem, l4re_ds_size(bild), &png_w, &png_h);
55
56   printf("PNG: %dx%d\n", png_w, png_h);
57
58   if (png_w < 0 || png_h < 0)
59     {
60       printf("Error with picture. Is it one?\n");
61       return 1;
62     }
63
64   if ((unsigned)png_w > fbi.width || (unsigned)png_h > fbi.height)
65     {
66       printf("Picture too large, cannot display\n");
67       return 1;
68     }
69
70   if (fbi.pixel_info.bytes_per_pixel == 2)
71     png_convert_RGB16bit_mem((void *)bildmem, (void *)vidmem,
72                              l4re_ds_size(bild),
73                              png_w*png_h*fbi.pixel_info.bytes_per_pixel,
74                              fbi.width);
75   else
76     png_convert_ARGB_mem((void *)bildmem, (void *)vidmem, l4re_ds_size(bild),
77                          png_w*png_h*fbi.pixel_info.bytes_per_pixel);
78
79   l4re_util_video_goos_fb_refresh(&gfb, 0, 0, png_w, png_h);
80
81   l4_sleep_forever();
82
83   return 0;
84 }