/src/libwebsockets/lib/misc/dlo/dlo-jpeg.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: JPEG |
25 | | */ |
26 | | |
27 | | #include <private-lib-core.h> |
28 | | #include "private-lib-drivers-display-dlo.h" |
29 | | |
30 | | void |
31 | | lws_display_dlo_jpeg_destroy(struct lws_dlo *dlo) |
32 | 0 | { |
33 | 0 | lws_dlo_jpeg_t *dlo_jpeg = lws_container_of(dlo, lws_dlo_jpeg_t, dlo); |
34 | |
|
35 | 0 | #if defined(LWS_WITH_CLIENT) && defined(LWS_WITH_SECURE_STREAMS) |
36 | 0 | lws_ss_destroy(&dlo_jpeg->flow.h); |
37 | 0 | #endif |
38 | 0 | lws_buflist_destroy_all_segments(&dlo_jpeg->flow.bl); |
39 | |
|
40 | 0 | if (dlo_jpeg->j) |
41 | 0 | lws_jpeg_free(&dlo_jpeg->j); |
42 | 0 | } |
43 | | |
44 | | lws_stateful_ret_t |
45 | | lws_display_render_jpeg(struct lws_display_render_state *rs) |
46 | 0 | { |
47 | 0 | lws_dlo_t *dlo = rs->st[rs->sp].dlo; |
48 | 0 | lws_dlo_jpeg_t *dlo_jpeg = lws_container_of(dlo, lws_dlo_jpeg_t, dlo); |
49 | 0 | lws_display_colour_t pc; |
50 | 0 | lws_fx_t ax, ay, t, t1; |
51 | 0 | lws_stateful_ret_t r; |
52 | 0 | const uint8_t *pix; |
53 | 0 | int s, e; |
54 | |
|
55 | 0 | lws_fx_add(&ax, &rs->st[rs->sp].co.x, &dlo->box.x); |
56 | 0 | lws_fx_add(&t, &ax, &dlo->box.w); |
57 | 0 | lws_fx_add(&ay, &rs->st[rs->sp].co.y, &dlo->box.y); |
58 | 0 | lws_fx_add(&t1, &ay, &dlo->box.h); |
59 | |
|
60 | 0 | if (!lws_jpeg_get_height(dlo_jpeg->j)) { |
61 | 0 | lwsl_info("%s: jpeg does not have dimensions yet\n", __func__); |
62 | 0 | return LWS_SRET_WANT_INPUT; |
63 | 0 | } |
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_jpeg_get_height(dlo_jpeg->j)) |
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_jpeg->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_jpeg_emit_next_line(dlo_jpeg->j, &pix, &dlo_jpeg->flow.data, |
91 | 0 | &dlo_jpeg->flow.len, rs->html == 1); |
92 | |
|
93 | 0 | if (r & LWS_SRET_NO_FURTHER_IN) |
94 | 0 | dlo_jpeg->flow.state = LWSDLOFLOW_STATE_READ_COMPLETED; |
95 | |
|
96 | 0 | if (r & LWS_SRET_FATAL || r == LWS_SRET_OK) |
97 | 0 | return r; |
98 | | |
99 | 0 | r = lws_flow_req(&dlo_jpeg->flow); |
100 | 0 | if (r & LWS_SRET_WANT_INPUT) |
101 | 0 | return r; |
102 | |
|
103 | 0 | } while (!pix); |
104 | | |
105 | | /* |
106 | | * What's in pix is either 24-bit RGB 3 bytes/px, or 8-bit grayscale |
107 | | * 1 byte/px, we have to map it on to either 32-bit RGBA or 16-bit YA |
108 | | * composition buf |
109 | | */ |
110 | | |
111 | 0 | pix = pix + (( (unsigned int)(s - ax.whole) * |
112 | 0 | (lws_jpeg_get_pixelsize(dlo_jpeg->j) / 8))); |
113 | |
|
114 | 0 | while (s < e && s >= ax.whole && s < lws_fx_roundup(&t) && |
115 | 0 | (s - ax.whole) < (int)lws_jpeg_get_width(dlo_jpeg->j)) { |
116 | |
|
117 | 0 | if (lws_jpeg_get_pixelsize(dlo_jpeg->j) == 8) |
118 | 0 | pc = LWSDC_RGBA(pix[0], pix[0], pix[0], 255); |
119 | 0 | else |
120 | 0 | pc = LWSDC_RGBA(pix[0], pix[1], pix[2], 255); |
121 | |
|
122 | 0 | lws_surface_set_px(rs->ic, rs->line, s, &pc); |
123 | 0 | s++; |
124 | 0 | pix += lws_jpeg_get_pixelsize(dlo_jpeg->j) / 8; |
125 | 0 | } |
126 | |
|
127 | 0 | return LWS_SRET_OK; |
128 | 0 | } |
129 | | |
130 | | lws_stateful_ret_t |
131 | | lws_display_dlo_jpeg_metadata_scan(lws_dlo_jpeg_t *dlo_jpeg) |
132 | 0 | { |
133 | 0 | lws_stateful_ret_t r; |
134 | 0 | size_t l, l1; |
135 | 0 | const uint8_t *pix; |
136 | | |
137 | | /* |
138 | | * If we don't have the image metadata yet, provide small chunks of the |
139 | | * source data until we do have the image metadata, but small enough |
140 | | * we can't produce any decoded pixels too early. |
141 | | */ |
142 | |
|
143 | 0 | while (!lws_jpeg_get_height(dlo_jpeg->j) && dlo_jpeg->flow.len) { |
144 | 0 | l1 = l = dlo_jpeg->flow.len > 128 ? 128 : dlo_jpeg->flow.len; |
145 | |
|
146 | 0 | r = lws_jpeg_emit_next_line(dlo_jpeg->j, &pix, &dlo_jpeg->flow.data, &l, 1); |
147 | 0 | if (r >= LWS_SRET_FATAL) { |
148 | 0 | lwsl_err("%s: hdr parse failed %d\n", __func__, r); |
149 | 0 | return r; |
150 | 0 | } |
151 | | |
152 | 0 | dlo_jpeg->flow.len -= l1 - l; |
153 | |
|
154 | 0 | if (lws_jpeg_get_height(dlo_jpeg->j)) { |
155 | 0 | lwsl_info("jpeg: w %d, h %d\n", |
156 | 0 | lws_jpeg_get_width(dlo_jpeg->j), |
157 | 0 | lws_jpeg_get_height(dlo_jpeg->j)); |
158 | |
|
159 | 0 | return LWS_SRET_OK; |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | 0 | return LWS_SRET_WANT_INPUT; |
164 | 0 | } |
165 | | |
166 | | lws_dlo_jpeg_t * |
167 | | lws_display_dlo_jpeg_new(lws_displaylist_t *dl, lws_dlo_t *dlo_parent, |
168 | | lws_box_t *box) |
169 | 0 | { |
170 | 0 | lws_dlo_jpeg_t *dlo_jpeg = lws_zalloc(sizeof(*dlo_jpeg), __func__); |
171 | |
|
172 | 0 | if (!dlo_jpeg) |
173 | 0 | return NULL; |
174 | | |
175 | 0 | dlo_jpeg->j = lws_jpeg_new(); |
176 | 0 | if (!dlo_jpeg->j) |
177 | 0 | goto bail; |
178 | | |
179 | 0 | dlo_jpeg->dlo.box = *box; |
180 | 0 | dlo_jpeg->dlo.render = lws_display_render_jpeg; |
181 | 0 | dlo_jpeg->dlo._destroy = lws_display_dlo_jpeg_destroy; |
182 | |
|
183 | 0 | lws_display_dlo_add(dl, dlo_parent, &dlo_jpeg->dlo); |
184 | |
|
185 | 0 | return dlo_jpeg; |
186 | | |
187 | 0 | bail: |
188 | 0 | lwsl_err("%s: bailed\n", __func__); |
189 | 0 | if (dlo_jpeg->j) |
190 | 0 | lws_jpeg_free(&dlo_jpeg->j); |
191 | |
|
192 | 0 | lws_free(dlo_jpeg); |
193 | |
|
194 | 0 | return NULL; |
195 | 0 | } |