Coverage Report

Created: 2026-02-26 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebsockets/lib/misc/dlo/dlo-png.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: 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
  if (dlo_png->flow.h)
37
0
    lws_ss_destroy(&dlo_png->flow.h);
38
0
#endif
39
0
  lws_buflist_destroy_all_segments(&dlo_png->flow.bl);
40
41
0
  if (dlo_png->png)
42
0
    lws_upng_free(&dlo_png->png);
43
0
}
44
45
lws_stateful_ret_t
46
lws_display_render_png(struct lws_display_render_state *rs)
47
0
{
48
0
  lws_dlo_t *dlo = rs->st[rs->sp].dlo;
49
0
  lws_dlo_png_t *dlo_png = lws_container_of(dlo, lws_dlo_png_t, dlo);
50
0
  lws_fx_t ax, ay, t, t1;
51
0
  lws_display_colour_t pc;
52
0
  lws_stateful_ret_t r;
53
0
  const uint8_t *pix;
54
0
  int s, e;
55
56
0
  if (!lws_upng_get_height(dlo_png->png)) {
57
0
    if (dlo_png->flow.state == LWSDLOFLOW_STATE_READ_COMPLETED)
58
0
      return LWS_SRET_OK;
59
60
0
    lwsl_notice("%s: png %s does not have dimensions yet\n", __func__, dlo_png->name);
61
0
    if (rs->html == 2)
62
0
      return LWS_SRET_OK;
63
64
0
    return LWS_SRET_WANT_INPUT;
65
0
  }
66
67
0
  lws_fx_add(&ax, &rs->st[rs->sp].co.x, &dlo->box.x);
68
0
  lws_fx_add(&t, &ax, &dlo->box.w);
69
0
  lws_fx_add(&ay, &rs->st[rs->sp].co.y, &dlo->box.y);
70
0
  lws_fx_add(&t1, &ay, &dlo->box.h);
71
72
0
  s = ax.whole;
73
0
  e = lws_fx_roundup(&t);
74
75
0
  if (rs->curr > lws_fx_roundup(&t1))
76
0
    return LWS_SRET_OK;
77
78
0
  if (rs->curr - lws_fx_roundup(&ay) >
79
0
      (int)lws_upng_get_height(dlo_png->png))
80
0
    return LWS_SRET_OK;
81
82
0
  if (s < 0)
83
0
    s = 0;
84
0
  if (s > rs->ic->wh_px[0].whole)
85
0
    return LWS_SRET_OK; /* off to the right */
86
0
  if (e > rs->ic->wh_px[0].whole)
87
0
    e = rs->ic->wh_px[0].whole - 1;
88
0
  if (e <= 0)
89
0
    return LWS_SRET_OK; /* off to the left */
90
91
0
  do {
92
0
    if (lws_flow_feed(&dlo_png->flow))
93
      /* if he says WANT_INPUT, we have nothing in the buflist */
94
0
      return LWS_SRET_WANT_INPUT;
95
96
0
    pix = NULL;
97
0
    r = lws_upng_emit_next_line(dlo_png->png, &pix, &dlo_png->flow.data,
98
0
              &dlo_png->flow.len, rs->html == 1 /* hold at metadata */);
99
100
0
    if (r & LWS_SRET_NO_FURTHER_IN)
101
0
      dlo_png->flow.state = LWSDLOFLOW_STATE_READ_COMPLETED;
102
103
0
    if (r & (LWS_SRET_FATAL | LWS_SRET_YIELD)  || r == LWS_SRET_OK)
104
0
      return r;
105
106
0
    r = lws_flow_req(&dlo_png->flow);
107
0
    if (r & LWS_SRET_WANT_INPUT)
108
0
      return r;
109
110
0
  } while (!pix);
111
112
0
  pix = pix + (( (unsigned int)(s - ax.whole) *
113
0
      (lws_upng_get_pixelsize(dlo_png->png) / 8)));
114
115
0
  while (s < e && s >= ax.whole && s < lws_fx_roundup(&t) &&
116
0
         (s - ax.whole) < (int)lws_upng_get_width(dlo_png->png)) {
117
118
0
    if (lws_upng_get_pixelsize(dlo_png->png))
119
0
      pc = LWSDC_RGBA(pix[0], pix[0], pix[0], pix[1]);
120
121
0
    pc = LWSDC_RGBA(pix[0], pix[1], pix[2], pix[3]);
122
123
0
    lws_surface_set_px(rs->ic, rs->line, s, &pc);
124
125
0
    s++;
126
0
    pix += lws_upng_get_pixelsize(dlo_png->png) / 8;
127
0
  }
128
129
0
  return LWS_SRET_OK;
130
0
}
131
132
lws_stateful_ret_t
133
lws_display_dlo_png_metadata_scan(lws_dlo_png_t *dlo_png)
134
0
{
135
0
  lws_stateful_ret_t r;
136
0
  size_t l, l1;
137
0
  const uint8_t *pix;
138
139
  /*
140
   * If we don't have the image metadata yet, provide small chunks of the
141
   * source data until we do have the image metadata, but small enough
142
   * we can't produce any decoded pixels too early.
143
   */
144
145
0
  while (!lws_upng_get_height(dlo_png->png) && dlo_png->flow.len) {
146
0
    l1 = l = dlo_png->flow.len > 33 ? 33 : dlo_png->flow.len;
147
148
0
    r = lws_upng_emit_next_line(dlo_png->png, &pix, &dlo_png->flow.data, &l, 1);
149
0
    if (r & LWS_SRET_FATAL) {
150
0
      lwsl_err("%s: %s: hdr parse failed\n", __func__, dlo_png->name);
151
0
      return r;
152
0
    }
153
154
0
    dlo_png->flow.len -= l1 - l;
155
156
0
    if (lws_upng_get_height(dlo_png->png)) {
157
0
      lwsl_info("png: w %d, h %d\n",
158
0
          lws_upng_get_width(dlo_png->png),
159
0
          lws_upng_get_height(dlo_png->png));
160
0
      return LWS_SRET_OK;
161
0
    }
162
0
  }
163
164
0
  return LWS_SRET_WANT_INPUT;
165
0
}
166
167
lws_dlo_png_t *
168
lws_display_dlo_png_new(lws_displaylist_t *dl, lws_dlo_t *dlo_parent,
169
      lws_box_t *box, const char *name, size_t len)
170
0
{
171
0
  lws_dlo_png_t *dlo_png = lws_zalloc(sizeof(*dlo_png), __func__);
172
173
0
  if (!dlo_png)
174
0
    return NULL;
175
176
0
  dlo_png->png = lws_upng_new();
177
0
  if (!dlo_png->png)
178
0
    goto bail;
179
180
181
0
  lws_strnncpy(dlo_png->name, name, len, sizeof(dlo_png->name));
182
0
  dlo_png->dlo.box = *box;
183
0
  dlo_png->dlo.render = lws_display_render_png;
184
0
  dlo_png->dlo._destroy = lws_display_dlo_png_destroy;
185
186
0
  lws_display_dlo_add(dl, dlo_parent, &dlo_png->dlo);
187
188
0
  return dlo_png;
189
190
0
bail:
191
0
  if (dlo_png->png)
192
0
    lws_upng_free(&dlo_png->png);
193
0
  lws_free(dlo_png);
194
195
  return NULL;
196
0
}