timage.c - ltk - Socket-based GUI for X11 (WIP)
HTML git clone git://lumidify.org/ltk.git (fast, but not encrypted)
HTML git clone https://lumidify.org/git/ltk.git (encrypted, but very slow)
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
timage.c (3249B)
---
1 /*
2 * Copyright (c) 2023 lumidify <nobody@lumidify.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <Imlib2.h>
18
19 #include "ltk.h"
20 #include "image.h"
21 #include "memory.h"
22 #include "graphics.h"
23 #include "xlib_shared.h"
24
25 struct ltk_image {
26 Imlib_Image img;
27 };
28
29 void
30 ltk_image_init(ltk_renderdata *data, size_t cache_bytes) {
31 /* FIXME: overflow */
32 imlib_set_cache_size((int)cache_bytes);
33 imlib_set_color_usage(128);
34 imlib_context_set_blend(0);
35 imlib_context_set_dither(1);
36 imlib_context_set_display(data->dpy);
37 imlib_context_set_visual(data->vis);
38 imlib_context_set_colormap(data->cm);
39 }
40
41 ltk_image *
42 ltk_image_create_from_mem(const char *path, const char *data, size_t sz) {
43 Imlib_Image img = imlib_load_image_mem(path, data, sz);
44 if (!img)
45 return NULL;
46 ltk_image *image = ltk_malloc(sizeof(ltk_image));
47 image->img = img;
48 return image;
49 }
50
51 ltk_image *
52 ltk_image_create_cropped_scaled(
53 ltk_image *image, int src_x, int src_y,
54 int src_w, int src_h, int dst_w, int dst_h) {
55 imlib_context_set_image(image->img);
56 /* FIXME: check since when supported */
57 Imlib_Image img = imlib_create_cropped_scaled_image(
58 src_x, src_y, src_w, src_h, dst_w, dst_h
59 );
60 if (!img)
61 return NULL;
62 ltk_image *new_image = ltk_malloc(sizeof(ltk_image));
63 new_image->img = img;
64 return new_image;
65 }
66
67 void
68 ltk_image_draw(ltk_image *image, ltk_surface *draw_surf, int x, int y) {
69 imlib_context_set_image(image->img);
70 imlib_context_set_drawable(ltk_surface_get_drawable(draw_surf));
71 imlib_render_image_on_drawable(x, y);
72 }
73
74 void
75 ltk_image_draw_scaled(
76 ltk_image *image, ltk_surface *draw_surf,
77 int x, int y, int w, int h) {
78 imlib_context_set_image(image->img);
79 imlib_context_set_drawable(ltk_surface_get_drawable(draw_surf));
80 imlib_render_image_on_drawable_at_size(x, y, w, h);
81 }
82
83 void
84 ltk_image_draw_cropped_scaled(
85 ltk_image *image, ltk_surface *draw_surf,
86 int src_x, int src_y, int src_w, int src_h,
87 int dst_x, int dst_y, int dst_w, int dst_h) {
88 imlib_context_set_image(image->img);
89 imlib_context_set_drawable(ltk_surface_get_drawable(draw_surf));
90 imlib_render_image_part_on_drawable_at_size(
91 src_x, src_y, src_w, src_h, dst_x, dst_y, dst_w, dst_h
92 );
93 }
94
95 int
96 ltk_image_get_width(ltk_image *image) {
97 imlib_context_set_image(image->img);
98 return imlib_image_get_width();
99 }
100
101 int
102 ltk_image_get_height(ltk_image *image) {
103 imlib_context_set_image(image->img);
104 return imlib_image_get_height();
105 }
106
107 void
108 ltk_image_destroy(ltk_image *image) {
109 imlib_context_set_image(image->img);
110 imlib_free_image();
111 ltk_free(image);
112 }