KolibriLib
Loading...
Searching...
No Matches
image.hpp
1#ifndef __IMAGE_H__
2#define __IMAGE_H__
3
4#include "UI.hpp"
5#include "small.hpp"
6#include "filesystem.hpp"
7#include "color.hpp"
8
9#include <string>
10#include <stdlib.h>
11#include <stdio.h>
12#include <kolibri_libimg.h>
13
14namespace KolibriLib
15{
16 namespace UI
17 {
19 namespace Images
20 {
22 class image : public UIElement
23 {
24 private:
25 Image *_img;
26
27 public:
33 image(const Coord& coord = {0, 0}, const Size& size = {100, 100});
34 ~image();
35
38 void LoadImage(const std::string &Path = DefaultImage);
39
43 void Render(Size size = {0, 0});
44 };
45
46 image::image(const Coord& coord, const Size& size) : UIElement(coord, size)
47 {
48 _ksys_debug_puts("Image:");
49 }
50
51 image::~image()
52 {
53 }
54
55 void image::LoadImage(const std::string &Path)
56 {
57 int32_t img_size;
58 FILE *f = fopen(Path.c_str(), "rb"); //Этот код взят из примера из /contrib/C_Layer/EXAMPLE/img_example/main.c
59
60 if (!f)
61 {
62 char *a = "LoadImage: Can't open file: ";
63 strcat(a, Path.c_str());
64 strcat(a, "\n");
65 _ksys_debug_puts(a);
66 }
67
68 if (fseek(f, 0, SEEK_END))
69 {
70 char *a = "Can't SEEK_END file: ";
71 strcat(a, Path.c_str());
72 strcat(a, "\n");
73 _ksys_debug_puts(a);
74 }
75
76 int filesize = ftell(f);
77 rewind(f);
78 char *fdata = (char *)malloc(filesize);
79
80 if (!fdata)
81 {
82 char *a = "No memory for file ";
83 strcat(a, Path.c_str());
84 strcat(a, "\n");
85 _ksys_debug_puts("malloc not return ptr");
86 }
87
88 img_size = (int32_t)fread(fdata, 1, filesize, f);
89
90 if (ferror(f))
91 {
92 char *a = "Error reading file ";
93 strcat(a, Path.c_str());
94 strcat(a, "\n");
95 _ksys_debug_puts(a);
96 }
97
98 fclose(f);
99
100
101
102
103 _img = img_decode( (void *)fdata, img_size, 0 );
104
105
106
107 free(fdata);
108
109
110
111 if (_img->Type != IMAGE_BPP32)
112 {
113 _img = img_convert(_img, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32
114 if (!_img)
115 {
116 _ksys_debug_puts("Convert error");
117 }
118 }
119 }
120
122 {
123 if (size.x != 0 && size.y != 0)
124 {
125 Image *img = img_scale(_img, 0, 0, _size.x, _size.y, NULL, LIBIMG_SCALE_STRETCH, LIBIMG_INTER_BILINEAR, size.x, size.y);
126 img_draw(img, _coord.x, _coord.y, size.x, size.y, 0, 0);
127 }
128 else
129 {
130 img_draw(_img, _coord.x, _coord.y, _size.x, _size.y, 0, 0);
131 }
132 }
133
134 } // namespace Image
135 } // namespace UI
136
137} // namespace KolibriLib
138
139
140#endif // __IMAGE_H__
Картинка как элемент интерфейса
Definition image.hpp:23
image(const Coord &coord={0, 0}, const Size &size={100, 100})
Конструктор
Definition image.hpp:46
void Render(Size size={0, 0})
Вывести изображение в окно
Definition image.hpp:121
void LoadImage(const std::string &Path=DefaultImage)
Загрузить изображение
Definition image.hpp:55
Элемент интерфейса
Definition UI.hpp:29
Size _size
Размер
Definition UI.hpp:36
Coord _coord
Координаты
Definition UI.hpp:33
Основное пространство имён
Definition base.hpp:18