Skip to content

Commit

Permalink
src/: fix test_4254() valgrind failure.
Browse files Browse the repository at this point in the history
We use new extra.pixmap_copy(), a fixed version of mupdf.ll_fz_pixmap_copy().
  • Loading branch information
julian-smith-artifex-com committed Feb 11, 2025
1 parent 82e1167 commit b67412c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9870,10 +9870,9 @@ def __init__(self, *args):

# copy samples data ------------------------------------------
if 1:
# We use specially-provided (by MuPDF Python bindings)
# ll_fz_pixmap_copy() to get best performance.
# We use our pixmap_copy() to get best performance.
# test_pixmap.py:test_setalpha(): 3.9s t=0.0062
mupdf.ll_fz_pixmap_copy( pm.m_internal, src_pix.m_internal, n)
extra.pixmap_copy( pm.m_internal, src_pix.m_internal, n)
elif 1:
# Use memoryview.
# test_pixmap.py:test_setalpha(): 4.6 t=0.51
Expand Down
58 changes: 58 additions & 0 deletions src/extra.i
Original file line number Diff line number Diff line change
Expand Up @@ -4039,6 +4039,58 @@ no_more_matches:;
return quads;
}

void pixmap_copy( fz_pixmap* pm, const fz_pixmap* src, int n)
{
assert(pm->w == src->w);
assert(pm->h == src->h);
assert(n <= pm->n);
assert(n <= src->n);

if (pm->n == src->n)
{
// identical samples
assert(pm->stride == src->stride);
memcpy(pm->samples, src->samples, pm->w * pm->h * pm->n);
}
else
{
int nn;
int do_alpha;
if (pm->n > src->n)
{
assert(pm->n == src->n + 1);
nn = src->n;
assert(!src->alpha);
assert(pm->alpha);
do_alpha = 1;
}
else
{
assert(src->n == pm->n + 1);
nn = pm->n;
assert(src->alpha);
assert(!pm->alpha);
do_alpha = 0;
}
for (int y=0; y<pm->h; ++y)
{
for (int x=0; x<pm->w; ++x)
{
memcpy(
pm->samples + pm->stride * y + pm->n * x,
src->samples + src->stride * y + src->n * x,
nn
);
if (do_alpha)
{
pm->samples[pm->stride * y + pm->n * x + pm->n-1] = 255;
}
}
}
}
}


%}

/* Declarations for functions defined above. */
Expand Down Expand Up @@ -4162,3 +4214,9 @@ int pixmap_n(mupdf::FzPixmap& pixmap);
PyObject* JM_search_stext_page(fz_stext_page *page, const char *needle);

PyObject *set_pixel(fz_pixmap* pm, int x, int y, PyObject *color);

/* Copies from <src> to <pm>, which must have same width and height. pm->n -
src->n must be -1, 0 or +1. If -1, <src> must have alpha and <pm> must not have
alpha, and we copy the non-alpha bytes. If +1 <src> must not have alpha and
<pm> must have alpha and we set <pm>'s alpha bytes all to 255.*/
void pixmap_copy(fz_pixmap* pm, const fz_pixmap* src, int n);

0 comments on commit b67412c

Please sign in to comment.