SayoriOS  0.3.3
widget_image.c
1 #include <kernel.h>
2 #include "desktop/widget.h"
3 #include "desktop/widget_image.h"
4 #include "fmt/tga.h"
5 #include "io/serial_port.h"
6 #include "io/rgb_image.h"
7 #include "sys/pixfmt.h"
8 
9 void destroy_widget_image(Widget_t* widget);
10 
11 void widget_image_renderer(struct Widget* this, struct Window* window) {
12  Widget_Image_t* this_data = (Widget_Image_t*)(this->custom_widget_data);
13 
14  if(this_data->image_data == NULL)
15  return;
16 
17  size_t real_width = this_data->meta.w;
18  size_t real_height = this_data->meta.h;
19 
20  draw_rgb_image(this_data->image_data, real_width, real_height, 32, this->x, this->y);
21 // drawRect(this->x, this->y, real_width, real_height, 0xff0000);
22 }
23 
24 Widget_t* new_widget_image(const char *path) {
25  Widget_t* wgt = new_bare_widget(
26  &widget_image_renderer,
27  &destroy_widget_image,
28  0, 0,
29  0, 0
30  );
31 
32  tga_header_t hdr;
33 
34  bool ok = tga_extract_info(path, &hdr);
35 
36  wgt->custom_widget_data = kcalloc(sizeof(Widget_Image_t), 1);
37  Widget_Image_t* wgt_data = (Widget_Image_t*)wgt->custom_widget_data;
38 
39  if(!ok) {
40  wgt_data->loaded = false;
41 
42  return wgt;
43  }
44 
45  wgt_data->path = path;
46  wgt_data->display_mode = NORMAL;
47  wgt_data->meta = hdr;
48 
49  wgt->width = hdr.w;
50  wgt->height = hdr.h;
51 
52  void* buffer_image = kcalloc(hdr.w * hdr.h, 4);
53  wgt_data->image_data = buffer_image;
54 
55  tga_extract_pixels(path, buffer_image);
56 
57  pixfmt_conv(buffer_image, 32, hdr.w, hdr.h, SCREEN_BGR, SCREEN_RGB);
58 
59 // hexview_advanced(buffer_image, 512, 24, true, new_qemu_printf);
60 
61  qemu_ok("Loaded successfully!");
62 
63  return wgt;
64 }
65 
66 void destroy_widget_image(Widget_t* widget) {
67  Widget_Image_t* wgt_data = (Widget_Image_t*)(widget->custom_widget_data);
68 
69  kfree(wgt_data->image_data);
70  kfree(widget->custom_widget_data);
71 
72  qemu_ok("Destroyed image widget");
73 }
Definition: widget.h:12
Definition: window.h:25