/src/libwebsockets/lib/misc/dlo/dlo-png.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * lws abstract display |
3 | | * |
4 | | * Copyright (C) 2019 - 2022 Andy Green <andy@warmcat.com> |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to |
8 | | * deal in the Software without restriction, including without limitation the |
9 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
10 | | * sell copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
22 | | * IN THE SOFTWARE. |
23 | | * |
24 | | * Display List Object: PNG |
25 | | */ |
26 | | |
27 | | #include <private-lib-core.h> |
28 | | #include "private-lib-drivers-display-dlo.h" |
29 | | |
30 | | void |
31 | | lws_display_dlo_png_destroy(struct lws_dlo *dlo) |
32 | 0 | { |
33 | 0 | lws_dlo_png_t *dlo_png = lws_container_of(dlo, lws_dlo_png_t, dlo); |
34 | |
|
35 | 0 | #if defined(LWS_WITH_CLIENT) && defined(LWS_WITH_SECURE_STREAMS) |
36 | 0 | lws_ss_destroy(&dlo_png->flow.h); |
37 | 0 | #endif |
38 | 0 | lws_buflist_destroy_all_segments(&dlo_png->flow.bl); |
39 | |
|
40 | 0 | if (dlo_png->png) |
41 | 0 | lws_upng_free(&dlo_png->png); |
42 | 0 | } |
43 | | |
44 | | lws_stateful_ret_t |
45 | | lws_display_render_png(struct lws_display_render_state *rs) |
46 | 0 | { |
47 | 0 | lws_dlo_t *dlo = rs->st[rs->sp].dlo; |
48 | 0 | lws_dlo_png_t *dlo_png = lws_container_of(dlo, lws_dlo_png_t, dlo); |
49 | 0 | lws_fx_t ax, ay, t, t1; |
50 | 0 | lws_display_colour_t pc; |
51 | 0 | lws_stateful_ret_t r; |
52 | 0 | const uint8_t *pix; |
53 | 0 | int s, e; |
54 | |
|
55 | 0 | if (!lws_upng_get_height(dlo_png->png)) { |
56 | 0 | lwsl_info("%s: png does not have dimensions yet\n", __func__); |
57 | 0 | return LWS_SRET_WANT_INPUT; |
58 | 0 | } |
59 | | |
60 | 0 | lws_fx_add(&ax, &rs->st[rs->sp].co.x, &dlo->box.x); |
61 | 0 | lws_fx_add(&t, &ax, &dlo->box.w); |
62 | 0 | lws_fx_add(&ay, &rs->st[rs->sp].co.y, &dlo->box.y); |
63 | 0 | lws_fx_add(&t1, &ay, &dlo->box.h); |
64 | |
|
65 | 0 | s = ax.whole; |
66 | 0 | e = lws_fx_roundup(&t); |
67 | |
|
68 | 0 | if (rs->curr > lws_fx_roundup(&t1)) |
69 | 0 | return LWS_SRET_OK; |
70 | | |
71 | 0 | if (rs->curr - lws_fx_roundup(&ay) > |
72 | 0 | (int)lws_upng_get_height(dlo_png->png)) |
73 | 0 | return LWS_SRET_OK; |
74 | | |
75 | 0 | if (s < 0) |
76 | 0 | s = 0; |
77 | 0 | if (s > rs->ic->wh_px[0].whole) |
78 | 0 | return LWS_SRET_OK; /* off to the right */ |
79 | 0 | if (e > rs->ic->wh_px[0].whole) |
80 | 0 | e = rs->ic->wh_px[0].whole - 1; |
81 | 0 | if (e <= 0) |
82 | 0 | return LWS_SRET_OK; /* off to the left */ |
83 | | |
84 | 0 | do { |
85 | 0 | if (lws_flow_feed(&dlo_png->flow)) |
86 | | /* if he says WANT_INPUT, we have nothing in the buflist */ |
87 | 0 | return LWS_SRET_WANT_INPUT; |
88 | | |
89 | 0 | pix = NULL; |
90 | 0 | r = lws_upng_emit_next_line(dlo_png->png, &pix, &dlo_png->flow.data, |
91 | 0 | &dlo_png->flow.len, rs->html == 1 /* hold at metadata */); |
92 | |
|
93 | 0 | if (r & LWS_SRET_NO_FURTHER_IN) |
94 | 0 | dlo_png->flow.state = LWSDLOFLOW_STATE_READ_COMPLETED; |
95 | |
|
96 | 0 | if (r & (LWS_SRET_FATAL | LWS_SRET_YIELD) || r == LWS_SRET_OK) |
97 | 0 | return r; |
98 | | |
99 | 0 | r = lws_flow_req(&dlo_png->flow); |
100 | 0 | if (r & LWS_SRET_WANT_INPUT) |
101 | 0 | return r; |
102 | |
|
103 | 0 | } while (!pix); |
104 | | |
105 | 0 | pix = pix + (( (unsigned int)(s - ax.whole) * |
106 | 0 | (lws_upng_get_pixelsize(dlo_png->png) / 8))); |
107 | |
|
108 | 0 | while (s < e && s >= ax.whole && s < lws_fx_roundup(&t) && |
109 | 0 | (s - ax.whole) < (int)lws_upng_get_width(dlo_png->png)) { |
110 | |
|
111 | 0 | if (lws_upng_get_pixelsize(dlo_png->png)) |
112 | 0 | pc = LWSDC_RGBA(pix[0], pix[0], pix[0], pix[1]); |
113 | |
|
114 | 0 | pc = LWSDC_RGBA(pix[0], pix[1], pix[2], pix[3]); |
115 | |
|
116 | 0 | lws_surface_set_px(rs->ic, rs->line, s, &pc); |
117 | |
|
118 | 0 | s++; |
119 | 0 | pix += lws_upng_get_pixelsize(dlo_png->png) / 8; |
120 | 0 | } |
121 | |
|
122 | 0 | return LWS_SRET_OK; |
123 | 0 | } |
124 | | |
125 | | lws_stateful_ret_t |
126 | | lws_display_dlo_png_metadata_scan(lws_dlo_png_t *dlo_png) |
127 | 0 | { |
128 | 0 | lws_stateful_ret_t r; |
129 | 0 | size_t l, l1; |
130 | 0 | const uint8_t *pix; |
131 | | |
132 | | /* |
133 | | * If we don't have the image metadata yet, provide small chunks of the |
134 | | * source data until we do have the image metadata, but small enough |
135 | | * we can't produce any decoded pixels too early. |
136 | | */ |
137 | |
|
138 | 0 | while (!lws_upng_get_height(dlo_png->png) && dlo_png->flow.len) { |
139 | 0 | l1 = l = dlo_png->flow.len > 33 ? 33 : dlo_png->flow.len; |
140 | |
|
141 | 0 | r = lws_upng_emit_next_line(dlo_png->png, &pix, &dlo_png->flow.data, &l, 1); |
142 | 0 | if (r & LWS_SRET_FATAL) { |
143 | 0 | lwsl_err("%s: hdr parse failed\n", __func__); |
144 | 0 | return r; |
145 | 0 | } |
146 | | |
147 | 0 | dlo_png->flow.len -= l1 - l; |
148 | |
|
149 | 0 | if (lws_upng_get_height(dlo_png->png)) { |
150 | 0 | lwsl_info("png: w %d, h %d\n", |
151 | 0 | lws_upng_get_width(dlo_png->png), |
152 | 0 | lws_upng_get_height(dlo_png->png)); |
153 | 0 | return LWS_SRET_OK; |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | 0 | return LWS_SRET_WANT_INPUT; |
158 | 0 | } |
159 | | |
160 | | lws_dlo_png_t * |
161 | | lws_display_dlo_png_new(lws_displaylist_t *dl, lws_dlo_t *dlo_parent, |
162 | | lws_box_t *box) |
163 | 0 | { |
164 | 0 | lws_dlo_png_t *dlo_png = lws_zalloc(sizeof(*dlo_png), __func__); |
165 | |
|
166 | 0 | if (!dlo_png) |
167 | 0 | return NULL; |
168 | | |
169 | 0 | dlo_png->png = lws_upng_new(); |
170 | 0 | if (!dlo_png->png) |
171 | 0 | goto bail; |
172 | | |
173 | 0 | dlo_png->dlo.box = *box; |
174 | 0 | dlo_png->dlo.render = lws_display_render_png; |
175 | 0 | dlo_png->dlo._destroy = lws_display_dlo_png_destroy; |
176 | |
|
177 | 0 | lws_display_dlo_add(dl, dlo_parent, &dlo_png->dlo); |
178 | |
|
179 | 0 | return dlo_png; |
180 | | |
181 | 0 | bail: |
182 | 0 | if (dlo_png->png) |
183 | 0 | lws_upng_free(&dlo_png->png); |
184 | 0 | lws_free(dlo_png); |
185 | |
|
186 | 0 | return NULL; |
187 | 0 | } |