libxputty 0.1
Loading...
Searching...
No Matches
xpngloader.c
Go to the documentation of this file.
1/*
2 * 0BSD
3 *
4 * BSD Zero Clause License
5 *
6 * Copyright (c) 2019 Hermann Meyer
7 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted.
10
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21#include "xpngloader.h"
22#include <stdint.h>
23
24/*
25 * @brief load png data from binary blob into cairo surface
26*/
27
28cairo_status_t png_stream_reader (void *_stream, unsigned char *data, unsigned int length) {
29 binary_stream * stream = (binary_stream *) _stream;
30 memcpy(data, &stream->data[stream->position],length);
31 stream->position += length;
32 return CAIRO_STATUS_SUCCESS;
33}
34
35cairo_surface_t *cairo_image_surface_create_from_stream ( const unsigned char* name) {
36 binary_stream png_stream;
37 png_stream.data = name;
38 png_stream.position = 0;
39 return cairo_image_surface_create_from_png_stream(&png_stream_reader, (void *)&png_stream);
40}
41
42void _copy_surface_to_widget(Widget_t *w, cairo_surface_t *getpng) {
43 int width = cairo_image_surface_get_width(getpng);
44 int height = cairo_image_surface_get_height(getpng);
45 cairo_surface_destroy(w->image);
46 w->image = NULL;
47
48 w->image = cairo_surface_create_similar (w->surface,
49 CAIRO_CONTENT_COLOR_ALPHA, width, height);
50 cairo_t *cri = cairo_create (w->image);
51 cairo_set_source_surface (cri, getpng,0,0);
52 cairo_paint (cri);
53 cairo_destroy(cri);
54}
55
56void _copy_scaled_surface_to_widget(Widget_t *w, cairo_surface_t *getpng) {
57 int width = cairo_image_surface_get_width(getpng);
58 int height = cairo_image_surface_get_height(getpng);
59 int width_t = w->scale.init_width;
60 int height_t = w->scale.init_height;
61 double x = (double)width_t/(double)width;
62 double y = (double)height_t/(double)height;
63 cairo_surface_destroy(w->image);
64 w->image = NULL;
65
66 w->image = cairo_surface_create_similar (w->surface,
67 CAIRO_CONTENT_COLOR_ALPHA, width_t, height_t);
68 cairo_t *cri = cairo_create (w->image);
69 cairo_scale(cri, x,y);
70 cairo_set_source_surface (cri, getpng,0,0);
71 cairo_paint (cri);
72 cairo_destroy(cri);
73}
74
75void widget_get_png(Widget_t *w, const unsigned char* name) {
76 cairo_surface_t *getpng = cairo_image_surface_create_from_stream (name);
77 _copy_surface_to_widget(w, getpng);
78 cairo_surface_destroy(getpng);
79}
80
81void widget_get_scaled_png(Widget_t *w, const unsigned char* name) {
82 cairo_surface_t *getpng = cairo_image_surface_create_from_stream (name);
84 cairo_surface_destroy(getpng);
85}
86
87void widget_get_png_from_file(Widget_t *w, const char* filename) {
88 cairo_surface_t *getpng = cairo_image_surface_create_from_png (filename);
89 _copy_surface_to_widget(w, getpng);
90 cairo_surface_destroy(getpng);
91}
92
93void widget_get_scaled_png_from_file(Widget_t *w, const char* filename) {
94 cairo_surface_t *getpng = cairo_image_surface_create_from_png (filename);
96 cairo_surface_destroy(getpng);
97}
98
100 w->image = wid->image;
101 w->flags |= REUSE_IMAGE;
102}
103
104cairo_surface_t * surface_get_png(Widget_t *w, cairo_surface_t *sf, const unsigned char* name) {
105 cairo_surface_t *getpng = cairo_image_surface_create_from_stream (name);
106 int width = cairo_image_surface_get_width(getpng);
107 int height = cairo_image_surface_get_height(getpng);
108
109 sf = cairo_surface_create_similar (w->surface,
110 CAIRO_CONTENT_COLOR_ALPHA, width, height);
111 cairo_t *cri = cairo_create (sf);
112 cairo_set_source_surface (cri, getpng,0,0);
113 cairo_paint (cri);
114 cairo_surface_destroy(getpng);
115 cairo_destroy(cri);
116 return sf;
117}
118
119void widget_set_icon_from_surface(Widget_t *w, cairo_surface_t *image) {
120#ifdef _WIN32 //PixmapIcon
121 return; // TODO
122#else
123 int width_t = cairo_xlib_surface_get_width(image);
124 int height_t = cairo_xlib_surface_get_height(image);
125 cairo_surface_t *icon = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width_t, height_t);
126 cairo_t *cri = cairo_create (icon);
127 cairo_set_source_surface (cri, image,0,0);
128 cairo_paint (cri);
129
130 int stride = cairo_image_surface_get_stride (icon);
131 unsigned long* icon_data = malloc(2+width_t*height_t * sizeof(unsigned long));
132 memset(icon_data, 0, 2+width_t*height_t * sizeof(unsigned long));
133 const unsigned char *data = cairo_image_surface_get_data(icon);
134 icon_data[0] = width_t;
135 icon_data[1] = height_t;
136 int x = 0;
137 int y = 0;
138 unsigned long* output_pixel = icon_data;
139 output_pixel += 2;
140 for (y = 0; y < height_t; y++) {
141 uint32_t *row = (uint32_t*) (data + y * stride);
142 for (x = 0; x < width_t; x++) {
143 output_pixel[0] |= row[x];
144 output_pixel ++;
145 }
146 }
147
148 Atom net_wm_icon = XInternAtom(w->app->dpy, "_NET_WM_ICON", False);
149 Atom cardinal = XInternAtom(w->app->dpy, "CARDINAL", False);
150 XChangeProperty(w->app->dpy, w->widget, net_wm_icon, cardinal, 32, PropModeReplace,
151 (const unsigned char*) icon_data, 2+width_t*height_t);
152
153 cairo_surface_destroy(icon);
154 cairo_destroy(cri);
155 free(icon_data);
156#endif
157}
158
159void widget_set_icon_from_png(Widget_t *w, const unsigned char* name) {
160#ifdef _WIN32 //PixmapIcon
161 return; // TODO
162#else
163 cairo_surface_t *image = cairo_image_surface_create_from_stream (name);
164 int width_t = cairo_image_surface_get_width(image);
165 int height_t = cairo_image_surface_get_height(image);
166 int stride = cairo_image_surface_get_stride (image);
167 unsigned long* icon_data = malloc(16+width_t*height_t * sizeof(unsigned long));
168 memset(icon_data, 0, 16+width_t*height_t * sizeof(unsigned long));
169 const unsigned char *data = cairo_image_surface_get_data(image);
170 icon_data[0] = width_t;
171 icon_data[1] = height_t;
172 int x = 0;
173 int y = 0;
174 unsigned long* output_pixel = icon_data;
175 output_pixel += 2;
176 for (y = 0; y < height_t; y++) {
177 uint32_t *row = (uint32_t*) (data + y * stride);
178 for (x = 0; x < width_t; x++) {
179 output_pixel[0] |= row[x];
180 output_pixel ++;
181 }
182 }
183
184 Atom net_wm_icon = XInternAtom(w->app->dpy, "_NET_WM_ICON", False);
185 Atom cardinal = XInternAtom(w->app->dpy, "CARDINAL", False);
186 XChangeProperty(w->app->dpy, w->widget, net_wm_icon, cardinal, 32, PropModeReplace,
187 (const unsigned char*) icon_data, 2+width_t*height_t);
188
189 cairo_surface_destroy(image);
190 free(icon_data);
191#endif
192}
193
194/*
195cairo_surface_t* iamge = cairo_image_surface_create_from_stream( LDVAR(image_name_png));
196*/
int init_height
Definition xwidget.h:355
int init_width
Definition xwidget.h:353
Widget_t - struct to hold the basic Widget_t info.
Definition xwidget.h:457
Resize_t scale
Definition xwidget.h:525
cairo_surface_t * image
Definition xwidget.h:491
Window widget
Definition xwidget.h:469
cairo_surface_t * surface
Definition xwidget.h:483
long long flags
Definition xwidget.h:461
Xputty * app
Definition xwidget.h:465
Display * dpy
Definition xputty.h:232
binary_stream - struct definition to read binary data into cairo surface
Definition xpngloader.h:104
long int position
Definition xpngloader.h:106
const unsigned char * data
Definition xpngloader.h:105
void widget_get_png_from_file(Widget_t *w, const char *filename)
widget_get_png_from_file - read png from file into Widget_t xlib surface
Definition xpngloader.c:87
void _copy_surface_to_widget(Widget_t *w, cairo_surface_t *getpng)
Definition xpngloader.c:42
cairo_status_t png_stream_reader(void *_stream, unsigned char *data, unsigned int length)
Definition xpngloader.c:28
void widget_get_scaled_png_from_file(Widget_t *w, const char *filename)
widget_get_scaled_png_from_file - read scaled png into Widget_t xlib surface
Definition xpngloader.c:93
void widget_get_scaled_png(Widget_t *w, const unsigned char *name)
widget_get_scaled_png - read scaled png into Widget_t xlib surface
Definition xpngloader.c:81
void widget_get_png(Widget_t *w, const unsigned char *name)
widget_get_png - read png into Widget_t xlib surface
Definition xpngloader.c:75
cairo_surface_t * surface_get_png(Widget_t *w, cairo_surface_t *sf, const unsigned char *name)
surface_get_png - read png into Widget_t xlib surface
Definition xpngloader.c:104
void _copy_scaled_surface_to_widget(Widget_t *w, cairo_surface_t *getpng)
Definition xpngloader.c:56
void widget_set_icon_from_png(Widget_t *w, const unsigned char *name)
widget_set_icon_from_png - set icon image from png binary to Widget_t those icon will be used in the ...
Definition xpngloader.c:159
void widget_set_icon_from_surface(Widget_t *w, cairo_surface_t *image)
widget_set_icon_from_surface - set icon image from cairo surface for Widget_t those icon will be used...
Definition xpngloader.c:119
cairo_surface_t * cairo_image_surface_create_from_stream(const unsigned char *name)
cairo_image_surface_create_from_stream - read binary data into cairo surface from stream
Definition xpngloader.c:35
void widget_get_surface_ptr(Widget_t *w, Widget_t *wid)
widget_get_surface_ptr - set pointer to a 2. Widget_t xlib surface
Definition xpngloader.c:99
unsigned long int Atom
@ REUSE_IMAGE
Definition xwidget.h:414