image_imlib.c (3419B)
1 /* 2 * Copyright (c) 2023-2024 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 <stddef.h> 18 19 #include <Imlib2.h> 20 21 #include "image.h" 22 #include "memory.h" 23 #include "graphics.h" 24 #include "graphics_xlib.h" 25 26 struct ltk_image { 27 Imlib_Image img; 28 }; 29 30 void 31 ltk_image_init(ltk_renderdata *data, size_t cache_bytes) { 32 /* FIXME: overflow */ 33 imlib_set_cache_size((int)cache_bytes); 34 imlib_set_color_usage(128); 35 imlib_context_set_blend(0); 36 imlib_context_set_dither(1); 37 imlib_context_set_display(data->dpy); 38 imlib_context_set_visual(data->vis); 39 imlib_context_set_colormap(data->cm); 40 } 41 42 ltk_image * 43 ltk_image_create_from_mem(const char *path, const char *data, size_t sz) { 44 Imlib_Image img = imlib_load_image_mem(path, data, sz); 45 if (!img) 46 return NULL; 47 ltk_image *image = ltk_malloc(sizeof(ltk_image)); 48 image->img = img; 49 return image; 50 } 51 52 ltk_image * 53 ltk_image_create_from_path(const char *path) { 54 Imlib_Image img = imlib_load_image_immediately(path); 55 if (!img) 56 return NULL; 57 ltk_image *image = ltk_malloc(sizeof(ltk_image)); 58 image->img = img; 59 return image; 60 } 61 62 ltk_image * 63 ltk_image_create_cropped_scaled( 64 ltk_image *image, int src_x, int src_y, 65 int src_w, int src_h, int dst_w, int dst_h) { 66 imlib_context_set_image(image->img); 67 /* FIXME: check since when supported */ 68 Imlib_Image img = imlib_create_cropped_scaled_image( 69 src_x, src_y, src_w, src_h, dst_w, dst_h 70 ); 71 if (!img) 72 return NULL; 73 ltk_image *new_image = ltk_malloc(sizeof(ltk_image)); 74 new_image->img = img; 75 return new_image; 76 } 77 78 void 79 ltk_image_draw(ltk_image *image, ltk_surface *draw_surf, int x, int y) { 80 imlib_context_set_image(image->img); 81 imlib_context_set_drawable(draw_surf->d); 82 imlib_render_image_on_drawable(x, y); 83 } 84 85 void 86 ltk_image_draw_scaled( 87 ltk_image *image, ltk_surface *draw_surf, 88 int x, int y, int w, int h) { 89 imlib_context_set_image(image->img); 90 imlib_context_set_drawable(draw_surf->d); 91 imlib_render_image_on_drawable_at_size(x, y, w, h); 92 } 93 94 void 95 ltk_image_draw_cropped_scaled( 96 ltk_image *image, ltk_surface *draw_surf, 97 int src_x, int src_y, int src_w, int src_h, 98 int dst_x, int dst_y, int dst_w, int dst_h) { 99 imlib_context_set_image(image->img); 100 imlib_context_set_drawable(draw_surf->d); 101 imlib_render_image_part_on_drawable_at_size( 102 src_x, src_y, src_w, src_h, dst_x, dst_y, dst_w, dst_h 103 ); 104 } 105 106 int 107 ltk_image_get_width(ltk_image *image) { 108 imlib_context_set_image(image->img); 109 return imlib_image_get_width(); 110 } 111 112 int 113 ltk_image_get_height(ltk_image *image) { 114 imlib_context_set_image(image->img); 115 return imlib_image_get_height(); 116 } 117 118 void 119 ltk_image_destroy(ltk_image *image) { 120 imlib_context_set_image(image->img); 121 imlib_free_image(); 122 ltk_free(image); 123 }