/src/libwebsockets/lib/misc/dlo/dlo-jpeg.c
Line | Count | Source |
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 | if (dlo_jpeg->flow.state == LWSDLOFLOW_STATE_READ_COMPLETED) |
62 | 0 | return LWS_SRET_OK; |
63 | | |
64 | 0 | lwsl_notice("%s: jpeg does not have dimensions yet\n", __func__); |
65 | 0 | if (rs->html == 2) |
66 | 0 | return LWS_SRET_OK; |
67 | | |
68 | 0 | return LWS_SRET_WANT_INPUT; |
69 | 0 | } |
70 | | |
71 | 0 | s = ax.whole; |
72 | 0 | e = lws_fx_roundup(&t); |
73 | |
|
74 | 0 | if (rs->curr > lws_fx_roundup(&t1)) |
75 | 0 | return LWS_SRET_OK; |
76 | | |
77 | 0 | if (rs->curr - lws_fx_roundup(&ay) > |
78 | 0 | (int)lws_jpeg_get_height(dlo_jpeg->j)) |
79 | 0 | return LWS_SRET_OK; |
80 | | |
81 | 0 | if (s < 0) |
82 | 0 | s = 0; |
83 | 0 | if (s > rs->ic->wh_px[0].whole) |
84 | 0 | return LWS_SRET_OK; /* off to the right */ |
85 | 0 | if (e > rs->ic->wh_px[0].whole) |
86 | 0 | e = rs->ic->wh_px[0].whole - 1; |
87 | 0 | if (e <= 0) |
88 | 0 | return LWS_SRET_OK; /* off to the left */ |
89 | | |
90 | 0 | do { |
91 | 0 | if (lws_flow_feed(&dlo_jpeg->flow)) |
92 | | /* if he says WANT_INPUT, we have nothing in the buflist */ |
93 | 0 | return LWS_SRET_WANT_INPUT; |
94 | | |
95 | 0 | pix = NULL; |
96 | 0 | r = lws_jpeg_emit_next_line(dlo_jpeg->j, &pix, &dlo_jpeg->flow.data, |
97 | 0 | &dlo_jpeg->flow.len, rs->html == 1); |
98 | |
|
99 | 0 | if (r & LWS_SRET_NO_FURTHER_IN) |
100 | 0 | dlo_jpeg->flow.state = LWSDLOFLOW_STATE_READ_COMPLETED; |
101 | |
|
102 | 0 | if (r & LWS_SRET_FATAL || r == LWS_SRET_OK) |
103 | 0 | return r; |
104 | | |
105 | 0 | r = lws_flow_req(&dlo_jpeg->flow); |
106 | 0 | if (r & LWS_SRET_WANT_INPUT) |
107 | 0 | return r; |
108 | |
|
109 | 0 | } while (!pix); |
110 | | |
111 | | /* |
112 | | * What's in pix is either 24-bit RGB 3 bytes/px, or 8-bit grayscale |
113 | | * 1 byte/px, we have to map it on to either 32-bit RGBA or 16-bit YA |
114 | | * composition buf |
115 | | */ |
116 | | |
117 | 0 | pix = pix + (( (unsigned int)(s - ax.whole) * |
118 | 0 | (lws_jpeg_get_pixelsize(dlo_jpeg->j) / 8))); |
119 | |
|
120 | 0 | while (s < e && s >= ax.whole && s < lws_fx_roundup(&t) && |
121 | 0 | (s - ax.whole) < (int)lws_jpeg_get_width(dlo_jpeg->j)) { |
122 | |
|
123 | 0 | if (lws_jpeg_get_pixelsize(dlo_jpeg->j) == 8) |
124 | 0 | pc = LWSDC_RGBA(pix[0], pix[0], pix[0], 255); |
125 | 0 | else |
126 | 0 | pc = LWSDC_RGBA(pix[0], pix[1], pix[2], 255); |
127 | |
|
128 | 0 | lws_surface_set_px(rs->ic, rs->line, s, &pc); |
129 | 0 | s++; |
130 | 0 | pix += lws_jpeg_get_pixelsize(dlo_jpeg->j) / 8; |
131 | 0 | } |
132 | |
|
133 | 0 | return LWS_SRET_OK; |
134 | 0 | } |
135 | | |
136 | | lws_stateful_ret_t |
137 | | lws_display_dlo_jpeg_metadata_scan(lws_dlo_jpeg_t *dlo_jpeg) |
138 | 0 | { |
139 | 0 | lws_stateful_ret_t r; |
140 | 0 | size_t l, l1; |
141 | 0 | const uint8_t *pix; |
142 | | |
143 | | /* |
144 | | * If we don't have the image metadata yet, provide small chunks of the |
145 | | * source data until we do have the image metadata, but small enough |
146 | | * we can't produce any decoded pixels too early. |
147 | | */ |
148 | |
|
149 | 0 | while (!lws_jpeg_get_height(dlo_jpeg->j) && dlo_jpeg->flow.len) { |
150 | 0 | l1 = l = dlo_jpeg->flow.len > 128 ? 128 : dlo_jpeg->flow.len; |
151 | |
|
152 | 0 | r = lws_jpeg_emit_next_line(dlo_jpeg->j, &pix, &dlo_jpeg->flow.data, &l, 1); |
153 | 0 | if (r >= LWS_SRET_FATAL) { |
154 | 0 | lwsl_err("%s: hdr parse failed %d\n", __func__, r); |
155 | 0 | return r; |
156 | 0 | } |
157 | | |
158 | 0 | dlo_jpeg->flow.len -= l1 - l; |
159 | |
|
160 | 0 | if (lws_jpeg_get_height(dlo_jpeg->j)) { |
161 | 0 | lwsl_info("jpeg: w %d, h %d\n", |
162 | 0 | lws_jpeg_get_width(dlo_jpeg->j), |
163 | 0 | lws_jpeg_get_height(dlo_jpeg->j)); |
164 | |
|
165 | 0 | return LWS_SRET_OK; |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | 0 | return LWS_SRET_WANT_INPUT; |
170 | 0 | } |
171 | | |
172 | | lws_dlo_jpeg_t * |
173 | | lws_display_dlo_jpeg_new(lws_displaylist_t *dl, lws_dlo_t *dlo_parent, |
174 | | lws_box_t *box, const char *name, size_t len) |
175 | 0 | { |
176 | 0 | lws_dlo_jpeg_t *dlo_jpeg = lws_zalloc(sizeof(*dlo_jpeg), __func__); |
177 | |
|
178 | 0 | if (!dlo_jpeg) |
179 | 0 | return NULL; |
180 | | |
181 | 0 | dlo_jpeg->j = lws_jpeg_new(); |
182 | 0 | if (!dlo_jpeg->j) |
183 | 0 | goto bail; |
184 | | |
185 | 0 | lws_strnncpy(dlo_jpeg->name, name, len, sizeof(dlo_jpeg->name)); |
186 | 0 | dlo_jpeg->dlo.box = *box; |
187 | 0 | dlo_jpeg->dlo.render = lws_display_render_jpeg; |
188 | 0 | dlo_jpeg->dlo._destroy = lws_display_dlo_jpeg_destroy; |
189 | |
|
190 | 0 | lws_display_dlo_add(dl, dlo_parent, &dlo_jpeg->dlo); |
191 | |
|
192 | 0 | return dlo_jpeg; |
193 | | |
194 | 0 | bail: |
195 | 0 | lwsl_err("%s: bailed\n", __func__); |
196 | 0 | if (dlo_jpeg->j) |
197 | 0 | lws_jpeg_free(&dlo_jpeg->j); |
198 | |
|
199 | 0 | lws_free(dlo_jpeg); |
200 | |
|
201 | | return NULL; |
202 | 0 | } |